JSON Examples

7- Retro: XML

Output

XML

<?xml version="1.0" encoding="UTF-8"?>
    <bookstore>
      <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
      </book>
      <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
      </book>
      <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>
    </bookstore>

HTML

<script>
    let xml = document.getElementById('xmldata').innerHTML.toString();
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(xml,"text/xml");
    let numBooks = xmlDoc.querySelectorAll('#store > book').length;

    xml = '';
    for(var i = 0; i < numBooks; i++) {
        xml += i+1 + '. ' +
            xmlDoc.querySelectorAll('#store > book > title')[i].innerHTML + '<br>'
    }

    document.getElementById("info").innerHTML = xml;
</script>

Equivalent JSON

{
   "@id": "store",
   "book": [
      {
         "@category": "cooking",
         "title": {
            "@lang": "en",
            "#text": "Everyday Italian"
         },
         "author": "Giada De Laurentiis",
         "year": "2005",
         "price": "30.00"
      },
      {
         "@category": "children",
         "title": {
            "@lang": "en",
            "#text": "Harry Potter"
         },
         "author": "J K. Rowling",
         "year": "2005",
         "price": "29.99"
      },
      {
         "@category": "web",
         "title": {
            "@lang": "en",
            "#text": "XQuery Kick Start"
         },
         "author": [
            "James McGovern",
            "Per Bothner",
            "Kurt Cagle",
            "James Linn",
            "Vaidyanathan Nagarajan"
         ],
         "year": "2003",
         "price": "49.99"
      },
      {
         "@category": "web",
         "@cover": "paperback",
         "title": {
            "@lang": "en",
            "#text": "Learning XML"
         },
         "author": "Erik T. Ray",
         "year": "2003",
         "price": "39.95"
      }
   ]
}