1

I want to parse xml output with javascript without jquery.

What is the best and practice about this problems?

4
  • 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? Commented Aug 30, 2013 at 17:13
  • 1
    What have you searched / tried / failed at / need help with? Commented Aug 30, 2013 at 17:14
  • MDN has a great article on XPath: developer.mozilla.org/en/docs/… Commented Aug 30, 2013 at 17:14
  • Here is an answer for another question about parsing xml with javascript stackoverflow.com/a/8412989/1804496 Commented Aug 30, 2013 at 17:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.