XML-to-JSON Conversion Library for Java

This section provides a tutorial example on how to use a 'JSON-java' conversion library offered by json.org to perform XML-to-JSON conversions in a Java program. The output JSON document is good, only if input XML document contains well structured data.

If you are familiar with Java language, you can try to use the "JSON-java" library to convert your XML document to a JSON document.

1. Go to "JSON-java" Website at https://github.com/stleary/JSON-java.

2. Click "Click here if you just want the latest release jar file" link and save the jar file, json-20200518.jar, to the current directory.

3. Create a Java program, Xml2Json.java, to use the "JSON-java" library to convert an XML document to a JSON document:

/* Xml2Json.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
import java.io.*;
import java.nio.file.*;
import org.json.XML;
import org.json.JSONObject;
public class Xml2Json {
  public static void main(String[] args) throws IOException {
    String input = args[0];
    String xml = new String(Files.readAllBytes(Paths.get(input)));
    JSONObject jsonObj = XML.toJSONObject(xml);
    int prettyPrintFactor = 2;
    String json = jsonObj.toString(prettyPrintFactor);

4. Run Xml2Json.java to convert our XML sample document, xml.html:

herong> java -cp json-20200518.jar Xml2Json.java xml.html 
{"html": {"body": {
  "p": [
    "A simple text paragraph.",
      "br": "",
      "em": "highlight text",
      "content": [
        "A complex paragraph with",
        "line breaks,",
        ", etc."
      "font": {
        "color": "#ff0000",
        "size": "+1",
        "content": "font changes"
  "bgcolor": "#dddddd",
  "h4": "My XML Web Page"

As you can see, this conversion library also uses some different conversion rules:

Obviously, this conversion library does not preserve the structure of the sample XML document, which contains complex XML elements with multiple child text segments like HTML documents. You can not find any way to convert the JSON document generated above back to the original XML document.

However, if an XML document contains only structured data, the output is pretty good. You can convert products.xml used in the previous tutorial with Xml2Json.java to see the result.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

XML-JSON Document Conversion

 What Is JSON (JavaScript Object Notation)

 Convert XML Document to JSON Document

 XML-to-JSON Conversion Rules

 XML-to-JSON Conversion Tool at onlinexmltools.com

XML-to-JSON Conversion Library for Java

 XML-to-JSON Conversion Module for Python

 Convert JSON Document to XML Document

 JSON-to-XML Conversion Rules

 JSON-to-XML Conversion Tool at onlinexmltools.com

 JSON-to-XML Conversion Library for Java

 JSON-to-XML Conversion Module for Python

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials


 Full Version in PDF/EPUB