471,852 Members | 865 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 software developers and data experts.

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!!!

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>
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;
  9. var aPrint = document.getElementById("items");
  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 2025
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
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?

Dec 12 '07 #3
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
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
That's it, works perfectly.

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
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.