Using XML
-
I have a source file in XML which I need to read in with javascript. Now I use the XmlDocument object, but I find it very user-unfriendly. Is there a library someone can recommend that will do the job better? thanks!
V.
-
It would probably help if you explained exactly what the problem is. SDKs/libraries are generally designed to help developers solve problems; user-friendliness are rarely a consideration.
Right now I'm using constructs like this:
xml.getElementsByTagName("group")[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue;
to get a value from some part of the XML. But the XML could change slightly and so this goes wrong. I "could" check each and every node for the right value, but that would end up being terribly complicated as far as I can see. Mainly because it seems that a node is not only a tag, but also the value in the tag. I tried XML transforms (xsl) , but the transform to html didn't work. (I looked it up and apparently there are some issues with javascript and xml transforms) So simply put, I need to traverse the node tree (or "tag tree") in a simpler and easier way. obsolete to say XML is not really my thing ;-)
V.
-
Right now I'm using constructs like this:
xml.getElementsByTagName("group")[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue;
to get a value from some part of the XML. But the XML could change slightly and so this goes wrong. I "could" check each and every node for the right value, but that would end up being terribly complicated as far as I can see. Mainly because it seems that a node is not only a tag, but also the value in the tag. I tried XML transforms (xsl) , but the transform to html didn't work. (I looked it up and apparently there are some issues with javascript and xml transforms) So simply put, I need to traverse the node tree (or "tag tree") in a simpler and easier way. obsolete to say XML is not really my thing ;-)
V.
V. wrote:
xml.getElementsByTagName("group")[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue;
is exactly the sort of code that leads to trouble. You need to traverse in a logical fashion starting at the top node, and at each point check whether the child is the correct one you are looking for.
-
V. wrote:
xml.getElementsByTagName("group")[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue;
is exactly the sort of code that leads to trouble. You need to traverse in a logical fashion starting at the top node, and at each point check whether the child is the correct one you are looking for.
indeed, that's the whole point. I find this XmlDocument system far too complicated or I'm using it wrong somehow. I'm not against loops and if's/switch statements, but it would end up ridiculously complicated in my opinion. eg.
the value of this tag
if you get to the tag "mytag" you get 2 nodes: one for the "mytag" and one for the contents of mytag ("the value of this tag"). In my mind, I would call "getValue" or something on the mytag node to get the contents. In XmlDocument you need to call the childnode first before getValue. Which is just weird.
V.
-
I have a source file in XML which I need to read in with javascript. Now I use the XmlDocument object, but I find it very user-unfriendly. Is there a library someone can recommend that will do the job better? thanks!
V.
Modern versions of Javascript have
querySelector()
andquerySelectorAll()
, which are a lot easier to use than traversing the tree - check your documentation (and document object) to see if you have them. You can basically pick out nodes using CSS selectors. As an alternative there is XPath - it uses expressions to drill down into the tree, just not in the familiar CSS-way that querySelector and querySelectorAll do it. The querySelector/querySelectorAll functions are available in all the modern browsers, but it looks like XPath is in everything apart from Internet Explorer. -
Modern versions of Javascript have
querySelector()
andquerySelectorAll()
, which are a lot easier to use than traversing the tree - check your documentation (and document object) to see if you have them. You can basically pick out nodes using CSS selectors. As an alternative there is XPath - it uses expressions to drill down into the tree, just not in the familiar CSS-way that querySelector and querySelectorAll do it. The querySelector/querySelectorAll functions are available in all the modern browsers, but it looks like XPath is in everything apart from Internet Explorer.