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.
⇒ Convert XML to JSON with PHP
2017-08-17, 1740🔥, 0💬
Popular Posts:
Where to find tutorials on Using Azure API Management Publisher Dashboard? Here is a list of tutoria...
How to login to the Developer Portal internally by you as the publisher? Normally, the Developer Por...
FYIcenter JSON Validator and Formatter is an online tool that checks for syntax errors of JSON text ...
How to use "xsl-transform" Azure API Policy Statement? The "xsl-transform" Policy Statement allows y...
How to use the "set-variable" Policy Statement to create custom variables for an Azure API service o...