JSON-to-XML 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 JSON-to-XML conversions in 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 JSON document to an XML 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 a JSON document to an XML document:

/* Xml2Json.java
 * Copyright (c) 2018 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 json = new String(Files.readAllBytes(Paths.get(input)));
    JSONObject jsonObj = new JSONObject(json);
    String xml = XML.toString(jsonObj);
    System.out.println(xml);
  }
}

4. Run Xml2Json.java to convert our JSON sample document, sample.json:

herong> java -cp json-20200518.jar Json2Xml.java sample.json 

<dictionary>
  <word>
    <origin>null</origin>
    <name>
      <is acronym>true</is acronym>
      <value>DTD</value>
    </name>
    <update>
      <date>2020-01-01</date>
    </update>
    <definition>Document Type Definition</definition>
  </word>
  <word>
    <origin>docga</origin>
    <name>
      <is acronym>false</is acronym>
      <value>Dog</value>
    </name>
    <update>
      <date>1000-01-01</date>
    </update>
    <definition>A domesticated carnivorous mammal</definition>
  </word>
</dictionary>

As you can see, this conversion tool uses almost the same conversion rules as I suggested, except that special characters in the JSON property names are not replaced. For example, "is acronym" is used as an XML element name directly, which results an invalid XML document.

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

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB