Traversing XML with Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonecj
    New Member
    • Dec 2007
    • 6

    Traversing XML with Javascript

    Hi team I'm traversing an XML file with 3000 + records in it. I've now run into some serious performance issues as a consequence... Is there an alternative to the below code I'm using? Or is there something in here that is really slowing it down? Any help would be much appreciated, it is currerntly taking 15 seconds to traverse my XML file!!!

    XML FILE
    Code:
    <supplier>
    <id>001</id>
    <name>My Supplier</name>
    </supplier>
    <supplier>
    <id>002</id>
    <name>Another One</name>
    </supplier>
    JAVASCRIPT
    Code:
    xmlDoc.validateOnParse = false;
    xmlDoc.resolveExternals = false;
    xmlDoc.preserveWhiteSpace = false;
    xmlDoc.async = false;
    xmlDoc.load(suppliers.xml);
    xmlObj = xmlDoc.documentElement;
    
    // THIS IS THE DIV WHERE THE SUPPLIERS ARE PRINTED
    var aPrint = document.getElementById("items");
    
    // AND WE LOOP THROUGH ALL 3000 OF THEM HERE
    for (var i=0; i<xmlObj.childNodes.length;i++)
    {
    var sDiv = document.createElement("div");
    sDiv.setAttribute("id","item" + i);
    sDiv.innerHTML = sDiv.innerHTML + "<h2>" + xmlObj.childNodes(i).childNodes(0).text + "</h2>";
    sDiv.innerHTML = sDiv.innerHTML + "<h3>" + xmlObj.childNodes(i).childNodes(1).text + "</h3>";
    var sBreak = document.createElement("hr");
    sDiv.appendChild(sBreak);		
    aPrint.appendChild(sDiv);
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Unfortunately, javascript tends to be very slow. Have you tried using another tool such as XSLT instead?

    Comment

    • tonecj
      New Member
      • Dec 2007
      • 6

      #3
      Thanks for the tip! The data comes straight in using XSL, no loading time at all.

      Much appreciated!

      Last question for the day, I'm currently googling it with little luck, is there a way to use XSL to do a search on the XML file based on text in a textbox?

      Thanks

      Comment

      • tonecj
        New Member
        • Dec 2007
        • 6

        #4
        I've managed to get no where today with the search... Does anyone know how I can pass the value of a textbox to an xsl:param???

        What I'm trying to do now is:

        <xsl:if test="contains( name, 'MYSEARCHSTRING '">

        Where MYSEARCHSTRING is the value in a text box.

        Thanks again!

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Are you running the xsl transformation in javascript?

          Generally the syntax is like:
          Code:
          xslProc.addParameter("sortOrder", myvalue);

          Comment

          • tonecj
            New Member
            • Dec 2007
            • 6

            #6
            That's it, works perfectly.

            Thanks!

            Comment

            Working...