Zuble QML Types   |  Zuble C++ Classes   |  Table of Contents

ZXml2Json QML Type

This object converts Zuble configuration files into JSON objects. More...

Import Statement: import controller .

Detailed Description

The conversion algorithm is lossy beause the JSON format can't represent all possible XML object relationships and because text whitespace is compressed.

The advantage of using this object is you won't need to write XQuery code to retrieve XML file data. The limitation of using this object is your data MUST CONFORM to a set of rules listed below or else it can't be converted.

If your data doesn't conform you can either bring the data into conformance or write your own query to retrieve the data by using or extending the ZxQuery object.

The XML to JSON algorithm converts sequences of elements in XML files into arrays of JSON strings as follows:

XML elements with unique names within their parent scope are converted into properties of the same name within their parent JSON object, each containing either a string value or a JSON object. XML elements that have same-name siblings within their parent scope are converted into a JSON property of that name containing an array of strings or JSON objects.

XML elements that have no attributes and no child elements are converted to a JSON string property with a value equal to the XML element's concatenated, whitespace compressed text nodes.

XML elements with attributes or child elements are converted into JSON objects. XML attributes appear as a 'param' property containing a JSON object with a property name and string value for each XML attribute. XML text for such elements, if it exists after whitespace compression, appears in JSON as a 'value' property containing the element's concatenated text nodes (not including text of any child elements).

Doublequote characters in XML text are backslash-escaped.

XML conformity rules:

  1. Element names must conform to JSON property name format.
    GOOD: <price_code>
    BAD: <price-code>
  2. Reserved element names (DO NOT USE): "param" and "value"
    GOOD: <anything_else_JSON_conformant>
    BAD: <value>
    BAD: <param>
  3. All whitespace is converted to space characters, then trimmed and compressed.
    GOOD: <description>This text will not be changed.</description>
    BAD: <description>   This text:   whitespace will
                         be trimmed and compressed,
                         new lines and tabs will be lost
                         and consecutive spaces will become a single space.
         </description>
  4. Same-name child elements are converted to an array and must be grouped together.
    GOOD:
      <item>
           <name>teeshirt</name>
           <care>machine wash warm</care>
           <size code="S">
              <color>black</color>
              <color>gray</color>
           </size>
           <size code="M"/>
              <color>black</color>
              <color>gray</color>
           </size>
           <size code="L"/>
              <color>blue</color>
              <color weave="knit" lot="76">gray</color>
           </size>
      <item>
    
    BAD:
      <item>
           <name>teeshirt</name>
           <care>machine wash warm</care>
           <size code="S"/>
           <color>black</color>
           <color>gray</color>
           <size code="M"/>
           <color>black</color>
           <color>gray</color>
           <size code="L"/>
           <color>blue</color>
           <color weave="knit" lot="76">gray</color>
      <item>

EXAMPLE Javascript:

item.name === "teeshirt"
item.care === "machine wash warm"
item.size[0].param.code === "S"
item.size[0].color[0] === "black"
item.size[1].param.code === "M"
item.size[2].color[0] === "blue"
item.size[2].color[1].param.weave === "knit"
item.size[2].color[1].param.lot === "76"
item.size[2].color[1].value === "gray"