Tools, FAQ, Tutorials:
Mapping XML Elements with Mixed Contents to JSON Values
What is the standard to map XML elements with mixed contents to JSON values?
✍: FYIcenter.com
There seems to be no industry standard on how to map XML elements with mixed contents to JSON values.
But there are some commonly used conventions:
1. Converting text content in an XML complex element to a JSON object property. If a text content is detected in an XML complex element, it is converted to a JSON object property. The property name is set to "#text" and the property value is set to the text content. For example:
XML: <div id="1001" class="banner">Buy 1 Get 1 Free</div> JSON: { "div": { "@id": "1001", "@class": "banner", "#text": "Buy 1 Get 1 Free" } }
2. Converting text content after sibling XML sub elements. If a text content is mixed with sibling XML sub elements, it is converted after all other sibling sub elements are converted. In other words, the "#text" property is listed at the last property of the output JSON object. For example:
XML: <div>Buy 1 <b>Get 1 Free</b></div> JSON: { "div": { "b": "Get 1 Free", "#text": "Buy 1" } }
This is acceptable in most cases, since the text content is considered as less important than sibling sub elements.
3. Aggregating scattered XML text contents to a single JSON object property. If multiple pieces of XML text contents are scattered among sibling sub elements are detected, all scattered text contents are aggregated into a single JSON object property. The property name is set to "#text" and the property value is set to an array holding each scattered text content. For example:
XML: <p>The quick brown <b>fox</b> jumps over the lazy <b>dog</b>.</p> JSON: { "b": [ "fox", "dog" ], "#text": [ "The quick brown", "jumps over the lazy", "." ] }
Note that aggregating scattered XML text contents to a single JSON object property will damage the order of how text contents are listed relative to sibling sub elements. In case like an XML document representing an HTML file, this conversion will cause problems. But in most cases of XML documents representing structured data, text contents are unlikely to be mixed with sibling sub elements.
Â
⇒⇒JSON Tutorials
2017-08-17, 1272👍, 0💬
Popular Posts:
How to access Request body from "context.Request.Body" object in Azure API Policy? Request body is t...
What is the "__init__()" class method? The "__init__()" class method is a special method that will b...
What Is HTML? HTML (HyperText Markup Language) is the standard markup language for creating Web page...
How to use the JSON to XML Conversion Tool at utilities-online.info? If you want to try the JSON to ...
How to extend json.JSONEncoder class? I want to encode other Python data types to JSON. If you encod...