I have a variable string that contains well-formed and valid XML. I need to use JavaScript code to parse this feed.
How can I accomplish this using (browser-compatible) JavaScript code?
I have a variable string that contains well-formed and valid XML. I need to use JavaScript code to parse this feed. How can I accomplish this using (browser-compatible) JavaScript code? |
||||
|
Update: For a more correct answer see Tim Down's answer. Internet Explorer and, for example, Mozilla-based browsers expose different objects for XML parsing, so it's wise to use a JavaScript framework like jQuery to handle the cross-browsers differences. A really basic example is:
For more in-depth information, read the tutorial Easy XML Consumption using jQuery. Note: As pointed out in comments; jQuery does not really do any XML parsing whatsoever, it relies on the DOM innerHTML method and will parse it like it would any HTML so be careful when using HTML element names in your XML. But I think it works fairly good for simple XML 'parsing', but it's probably not suggested for intensive or 'dynamic' XML parsing where you do not upfront what XML will come down and this tests if everything parses as expected. |
|||||||||||||||||||||
|
Please take a look at XML DOM Parser (W3Schools). It's a tutorial on XML DOM parsing. The actual DOM parser differs from browser to browser but the DOM API is standardised and remains the same (more or less). Alternatively use E4X if you can restrict yourself to Firefox. It's relatively easier to use and it's part of JavaScript since version 1.6. Here is a small sample usage...
|
||||
|
Assuming you want to build a DOM from this XML, sadly you need to use browser-specific interfaces. Here is an attempt at a cross-browser function for doing it, however. W3Schools also document the individual browser-specific methods. |
|||
|
Most examples on the web (and some presented above) show how to load an XML from a file in a browser compatible manner. This proves easy, except in the case of Google Chrome which does not support the In your case, the scenario is different, because you want to load the XML from a string variable, not a URL. For this requirement however, Chrome supposedly works just like Mozilla (or so I've heard) and supports the parseFromString() method. Here is a function I use (it's part of the Browser compatibility library I'm currently building):
|
|||||||||||||
|
I've always used the approach below which works in IE and Firefox. Example XML:
JavaScript:
|
|||||
|
I created a jQuery plugin that parses XML pretty easily. It works in all Yahoo A grade browsers and comes with filtering, limit and callback options. It might be a solution to consider: http://jparse.kylerush.net/ |
|||
|
Apparently jQuery now provides jQuery.parseXML http://api.jquery.com/jQuery.parseXML/ as of version 1.5 |
|||
|
The following will parse an XML string into an XML document in all major browsers, including Internet Explorer 6. Once you have that, you can use the usual DOM traversal methods/properties such as
Example usage:
If you're using jQuery, from version 1.5 you can use its built-in
|
|||||||||||||||||||||
|
Marknote is a nice lightweight cross-browser JavaScript XML parser. It's object-oriented and it's got plenty of examples, plus the API is documented. It's fairly new, but it has worked nicely in one of my projects so far. One thing I like about it is that it will read XML directly from strings or URLs and you can also use it to convert the XML into JSON. Here's an example of what you can do with Marknote:
|
|||||
|