Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Using XML

Using XML

Scheduled Pinned Locked Moved JavaScript
javascriptxmlquestioncareer
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V 0
    wrote on last edited by
    #1

    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.

    L G 2 Replies Last reply
    0
    • V V 0

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      V 1 Reply Last reply
      0
      • L Lost User

        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.

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • V V 0

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 1 Reply Last reply
          0
          • L Lost User

            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 Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • V V 0

              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.

              G Offline
              G Offline
              Graham Breach
              wrote on last edited by
              #6

              Modern versions of Javascript have querySelector() and querySelectorAll(), 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.

              V 1 Reply Last reply
              0
              • G Graham Breach

                Modern versions of Javascript have querySelector() and querySelectorAll(), 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.

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                thanks! I'll certainly have a look at that.

                V.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups