Read attributes, text and repeated elements
The converter uses the following mapping:
| XML | JSON representation |
|---|---|
| Document root | One top-level property with the root name |
| Attribute | Property prefixed with @ |
| Text-only element without attributes | String value |
| Text in an element with attributes | #text property |
| Repeated sibling elements with the same name | Array |
| One child with a given name | One value, rather than a one-item array |
For example:
<catalog currency="USD">
<item id="p101"><name>Desk Lamp</name><price>32.00</price></item>
<item id="p102"><name>Travel Mug</name><price>19.90</price></item>
</catalog>
becomes:
{
"catalog": {
"@currency": "USD",
"item": [
{"@id":"p101","name":"Desk Lamp","price":"32.00"},
{"@id":"p102","name":"Travel Mug","price":"19.90"}
]
}
}
Prices remain strings. The converter does not assume that XML text is a number, date or boolean. In a consuming application, normalize the one-versus-many case before iterating over item.
