XML Tutorials - Herong's Tutorial Examples - v5.25, by Herong Yang
JSON-to-XML Conversion Rules
This section provides a simple set of JSON-to-XML Conversion Rules that preserves the structure of the original JSON document. You can build your own converter with those rules.
If you are a developer, you may want to implement your own JSON-to-XML converter so that you have the full control of the output.
To implement own JSON-to-XML converter, you need define a set of conversion rules first. Here is my suggestion to build a simple converter that preserves the original XML document structure.
1. Create a root element, <root>...</root>, to store the entire document converted from the JSON document.
2. For a JSON primitive value, convert the value as the text content of the current XML element. You can add XML Schema Instance type, xsi:string, xsi:float, or xsi:boolean, to identify its type, if you want to.
3. For a JSON Null value, convert the value as an empty text content of the current XML element. You can add XML Schema Instance type, xsi:nil, to identify its type, if you want to.
4. For a JSON Array value, convert the value to multiple XML elements by repeating the current XML element. Then loop through each item in the JSON Array to populate each XML element.
5. For a JSON Object value, convert the value to child element of the current XML element. Then loop through each property in the JSON object to populate each child element, by using the property name as the child element tag with "_" to replace any special characters.
Here is a sample XML document, xml.html, that we can use to validate our conversion rules. Note that some elements and attributes are deprecated in latest HTML specifications. But it is still a valid XML document.
{ "dictionary": { "word": [ { "update": { "date": "2020-01-01" }, "name": { "is acronym": true, "value": "DTD" }, "definition": "Document Type Definition", "origin": null }, { "update": { "date": "1000-01-01" }, "name": { "is acronym": false, "value": "Dog" }, "definition": "A domesticated carnivorous mammal", "origin": "docga" } ] } }
If you apply our conversion rules, the converted XML should be:
<root> <dictionary> <word> <name> <is_acronym>true</is_acronym> <value>DTD</value> </name> <update> <date>2020-01-01</date> </update> <definition>Document Type Definition</definition> <origin></origin> </word> <word> <name> <value>Dog</value> <is_acronym>false</is_acronym> </name> <update> <date>1000-01-01</date> </update> <definition>A domesticated carnivorous mammal</definition> <origin>docga</origin> </word> </dictionary> </root>
I think the output is almost perfect! The original JSON document structure is exactly preserved in the output XML document. But some JSON data type information, like number, boolean or null, is lost in the XML document. What do you think?
Exercise: Build an JSON-to-XML converter in your favorite language using above conversion rules.
Table of Contents
Introduction of XML (eXtensible Markup Language)
What Is JSON (JavaScript Object Notation)
Convert XML Document to JSON Document
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 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
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
Validating XML Documents Against Specified XML Schemas
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
XSLT Elements as Programming Statements
Control and Generate XML Element in the Result
PHP Extensions for XML Manipulation
Processing XML with Python Scripts
XML Tools Plugin for Notepad++
XML Plugin Packages for Atom Editor