admin管理员组

文章数量:1201025

Say I have this XML with about 1000+ bookinfo nodes.

<results>
  <books>
   <bookinfo>
        <name>1</dbname>
   </bookinfo>
   <bookinfo>
     <name>2</dbname>
   </bookinfo>
   <bookinfo>
     <name>3</dbname>
   </bookinfo>
 </books>
</results>

I'm currently using this to get the name of each book:

var books = this.req.responseXML.getElementsByTagName("books")[0].getElementsByTagName("bookinfo")

Then use a for loop to do something with each book name:

var bookName = books[i].getElementsByTagName("name")[0].firstChild.nodeValue;

I'm finding this really slow when books is really big. Unfortunately, there's no way to limit the result set nor specify a different return type.

Is there a faster way?

Say I have this XML with about 1000+ bookinfo nodes.

<results>
  <books>
   <bookinfo>
        <name>1</dbname>
   </bookinfo>
   <bookinfo>
     <name>2</dbname>
   </bookinfo>
   <bookinfo>
     <name>3</dbname>
   </bookinfo>
 </books>
</results>

I'm currently using this to get the name of each book:

var books = this.req.responseXML.getElementsByTagName("books")[0].getElementsByTagName("bookinfo")

Then use a for loop to do something with each book name:

var bookName = books[i].getElementsByTagName("name")[0].firstChild.nodeValue;

I'm finding this really slow when books is really big. Unfortunately, there's no way to limit the result set nor specify a different return type.

Is there a faster way?

Share Improve this question edited Feb 28, 2011 at 4:17 doremi asked Feb 28, 2011 at 4:10 doremidoremi 15.3k31 gold badges97 silver badges152 bronze badges 11
  • You can store the getElementsByTagName call as a variable/array so it only gets called once. – user1385191 Commented Feb 28, 2011 at 4:13
  • 100 XML nodes is nothing. Show how you're doing it for more than one book at a time. – Matt Ball Commented Feb 28, 2011 at 4:16
  • 1 Are you sure it's the parsing that's slow? Are you using a JavaScript profiler? How are you sure that it's not something else caused by a large XML file, like transporting it to the browser? – Matt Ball Commented Feb 28, 2011 at 4:20
  • 1 @Joshua I think @Matt is right - you should run your application in a browser that has profiler capabilities. – Pointy Commented Feb 28, 2011 at 4:22
  • 1 any chance to convert that XML to JSON in the server? you will save both bytes transferred and CPU time – gonchuki Commented Feb 28, 2011 at 4:33
 |  Show 6 more comments

3 Answers 3

Reset to default 12

You can try fast xml parser to convert XML data to JSON which is implemented in JS. Here is the benchmark against other parser.

var parser = require('fast-xml-parser');
var jsonObj = parser.parse(xmlData);

// when a tag has attributes
var options = {
        attrPrefix : "@_"        };
var jsonObj = parser.parse(xmlData,options);

If you don't want to use npm library, you can include parser.js in your HTML directly.

Disclaimer: I'm the author of this library.

Presumably you are using XMLHttpRequest, in which case the XML is parsed before you call any methods of responseXML (i.e. the XML has already been parsed and turned into a DOM). If you want a faster parser, you'll probably need a different user agent or a different javascript engine for your current UA.

If you want a faster way to access content in the XML document, consider XPath:

Mozilla documentation

MSDN documentation

I used an XPath expression (like //parentNode/node/text()) on a 134KB local file to extract the text node of 439 elements, put those into an array (because that's what my standard evalXPath() function does), then iterate over that array to put the nodeValue for each text node into another array, doing two replace calls with regular expressions to format the text, then alert() that to the screen with join('\n'). It took 3ms.

A 487KB file with 529 nodes took 4ms (IE 6 reported 15ms but its clock has very poor resolution). Of course my network latency will be nearly zero, but it shows that the XML parser, XPath evaluator and script in general can process that size file quickly.

if you want to parse the information from that xml much faster, try txml. it is very easy to use and for the type of xml you have shown, you can use its simplify method. it will give you very clean objects to work with.

https://www.npmjs.com/package/txml

Disclaimer: I'm the author of this library.

本文标签: javascriptFastest Way to Parse this XML in JSStack Overflow