1

I want to parse xml output with javascript without jquery.

What is the best and practice about this problems?

Roger Giuffre
  • 35
  • 1
  • 7
  • 3
    Where are you getting your output from? Why don't you want to use jQuery? What does your XML look like? Do you have a specific example of the problem you are facing? – Zack Aug 30 '13 at 17:13
  • 1
    What have you searched / tried / failed at / need help with? – tymeJV Aug 30 '13 at 17:14
  • MDN has a great article on XPath: https://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript – CodingIntrigue Aug 30 '13 at 17:14
  • Here is an answer for another question about parsing xml with javascript http://stackoverflow.com/a/8412989/1804496 – Zack Aug 30 '13 at 17:19

1 Answers1

5

you can use the DOMParser ( or ActiveX if is Internet Explorer ) like below...

if ( window.DOMParser ) { // Standard
    tmp = new DOMParser();
    xml = tmp.parseFromString( data , "text/xml" );
} else { // IE
    xml = new ActiveXObject( "Microsoft.XMLDOM" );
    xml.async = "false";
    xml.loadXML( data );
}

//the XML is a xml document now :D

to navigate you can use

xml.getElementsByTagName("tagName");
xml.querySelector("selector");
xml.querySelectorAll("[attr=value]");
//and others
Luan Castro
  • 1,184
  • 7
  • 14