2

How and/or in which conventions are XML attributes put in an object named "@attributes" when converting XML to JSON? This style is used in this generic XML parser:

obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
  var attribute = xml.attributes.item(j);
  obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}

..to create JSON like this:

...
elem_array = [
  {
    "@attributes": {
      an-attribute: "",
      another-one: "mr.text"
    }
  }
]
...

I'm not looking for answers about element-centric vs. attribute-centric XML design, unless those things are more closely related to my question than I thought of course. ;)

Where did the @attributes notation come from and are there reasons to use it over using your own notation?

Thanks!

Sephie
  • 331
  • 1
  • 3
  • 15

1 Answers1

2

it is likely evolved from the XPATH @attribute syntax

http://www.tizag.com/xmlTutorial/xpathattribute.php

Using the @ makes it recognisable to people used to XPATH

Example: XPath : Get nodes where child node contains an attribute

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Then using names like "@another-one":"mr.text" would make the attributes recognizable. Why put all attributes in one @attributes element? – Sephie Jun 05 '12 at 11:50
  • Because there are more than one attribute on the one xml tag? – mplungjan Jun 05 '12 at 11:51
  • 1
    I agree it's a clean way to group multiple attributes, but that tells us nothing about the origin of this style or if there's a reason we should choose this convention over any other one. – Sephie Jun 05 '12 at 12:09
  • It seems to have been chosen for us if we use the parser in question :) - Not sure what you want to get out of this question... – mplungjan Jun 05 '12 at 12:56
  • Good point! Maybe I wrongly assumed that it was a convention in the first place. Probably because of [link](http://stackoverflow.com/questions/5957371/is-use-of-attributes-in-json-non-standard-or-standard)this SO question. – Sephie Jun 05 '12 at 13:03
  • Right. I would not worry about it since any XPATH person would not have an issue with bla["@attributes"] – mplungjan Jun 05 '12 at 13:22