Hacker News new | ask | show | jobs
by p51ngh 2223 days ago
For your sample code Python xmltodict gives:

  {
    "bookstore": {
      "book": [
        {
          "@category": "cooking",
          "title": {
            "@lang": "en",
            "#text": "Everyday 1"
          },
          "author": "Giada De Laurentiis",
          "year": "2005",
          "price": "30.00"
        },
        {
          "@category": "web",
          "title": {
            "@lang": "en",
            "#text": "Learning XML"
          },
          "author": "Erik T. Ray",
          "year": "2003",
          "price": "39.95"
        }
      ]
    }
  }
Your code gives:

  [
    {
      "author" : "Giada De Laurentiis",
      "title" : {
        "lang" : "en",
        "text" : "Everyday 1"
      },
      "year" : "2005",
      "category" : "cooking",
      "price" : "30.00"
    },
    {
      "author" : "Erik T. Ray",
      "title" : {
        "lang" : "en",
        "text" : "Learning XML"
      },
      "year" : "2003",
      "category" : "web",
      "price" : "39.95"
    }
  ]
Which one's more accurate?!!
1 comments

Thanks for your question.

Both are correct, but if you think about it we have a Bookstore with multiple books so putting them in an array of objects makes more sense.. we don't need the root element name. xmltodict just puts extra unnecessary complexity