Skip to content

Mapping Convention

Christoph Beck edited this page Oct 21, 2011 · 5 revisions

The purpose of StAXON's mapping convention is to generate a more compact JSON. It borrows the "$" syntax for text elements from the Badgerfish convention but attempts to avoid needless text-only JSON objects. (However, StAXON's JsonXMLStreamReader can also handle Badgerfish-mapped documents out of the box.)

The rules are:

  • Element names become object properties.

      <alice/>
    

    becomes

      { "alice": null }
    
  • Attributes go in properties whose name begin with "@".

      <alice charlie="david"/>
    

    becomes

      { "alice": { "@charlie": "david" } }
    
  • Text-only elements go to a simple key/value property.

      <alice>bob</alice>
    

    becomes

      { "alice": "bob" }
    
  • Otherwise, text content is mapped to the "$" property.

      <alice charlie="david">bob</alice>
    

    becomes

      { "alice": { "@charlie": "david", "$": "bob" } }
    
  • Nested elements go to nested properties.

      <alice><bob>charlie</bob></alice>
    

    becomes

      { "alice": { "bob": "charlie"} }
    
  • A default namespace declaration goes in the element's "@xmlns" property.

      <alice xmlns="http://some-namespace"/>
    

    becomes

      { "alice": { "@xmlns": "http://some-namespace" } }
    
  • A prefixed namespace declaration goes in the element's "@xmlns:<prefix>" property.

      <edgar:alice xmlns:edgar="http://some-other-namespace>bob</edgar:alice>
    

    becomes

      { "edgar:alice": { "@xmlns:edgar": "http://some-other-namespace", "$": "bob" } }
    
  • Adjacent elements with the same name may become array elements.

      <alice><? xml-multiple bob ?><bob>charlie</bob><bob>david</bob></alice>
    

    becomes

      { "alice": { "bob": [ "charlie", "david" ] } }
    

    The <? xml-multiple bob ?> processing instruction serves as a hint to start an array of bob elements. Please refer to Mastering Arrays for more on this topic.

Clone this wiki locally