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, 1181👍, 0💬
Popular Posts:
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...
How To Control Padding Spaces within a Table Cell? Cell padding spaces are spaces between cell inner...
Where is API Management Service on my Azure Portal? If your IT department has signed up for an Azure...
How To Read a File in Binary Mode in PHP? If you have a file that stores binary data, like an execut...
How to create a new API on the Publisher Dashboard of an Azure API Management Service? If you are ne...