469,337 Members | 5,884 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,337 developers. It's quick & easy.

Traversing XML with Javascript

6
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
Expand|Select|Wrap|Line Numbers
  1. <supplier>
  2. <id>001</id>
  3. <name>My Supplier</name>
  4. </supplier>
  5. <supplier>
  6. <id>002</id>
  7. <name>Another One</name>
  8. </supplier>
JAVASCRIPT
Expand|Select|Wrap|Line Numbers
  1. xmlDoc.validateOnParse = false;
  2. xmlDoc.resolveExternals = false;
  3. xmlDoc.preserveWhiteSpace = false;
  4. xmlDoc.async = false;
  5. xmlDoc.load(suppliers.xml);
  6. xmlObj = xmlDoc.documentElement;
  7.  
  8. // THIS IS THE DIV WHERE THE SUPPLIERS ARE PRINTED
  9. var aPrint = document.getElementById("items");
  10.  
  11. // AND WE LOOP THROUGH ALL 3000 OF THEM HERE
  12. for (var i=0; i<xmlObj.childNodes.length;i++)
  13. {
  14. var sDiv = document.createElement("div");
  15. sDiv.setAttribute("id","item" + i);
  16. sDiv.innerHTML = sDiv.innerHTML + "<h2>" + xmlObj.childNodes(i).childNodes(0).text + "</h2>";
  17. sDiv.innerHTML = sDiv.innerHTML + "<h3>" + xmlObj.childNodes(i).childNodes(1).text + "</h3>";
  18. var sBreak = document.createElement("hr");
  19. sDiv.appendChild(sBreak);        
  20. aPrint.appendChild(sDiv);
  21. }
Dec 12 '07 #1
5 1966
jkmyoung
2,057 Expert 2GB
Unfortunately, javascript tends to be very slow. Have you tried using another tool such as XSLT instead?
Dec 12 '07 #2
tonecj
6
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
Dec 12 '07 #3
tonecj
6
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!
Dec 13 '07 #4
jkmyoung
2,057 Expert 2GB
Are you running the xsl transformation in javascript?

Generally the syntax is like:
Expand|Select|Wrap|Line Numbers
  1. xslProc.addParameter("sortOrder", myvalue);
Dec 13 '07 #5
tonecj
6
That's it, works perfectly.

Thanks!
Dec 17 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by lothar | last post: by
3 posts views Thread by Plamen Valtchev | last post: by
4 posts views Thread by plmanikandan | last post: by
30 posts views Thread by asit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.