Roger Giuffre
Roger Giuffre

Reputation: 35

Parse xml with javascript without jquery

I want to parse xml output with javascript without jquery.

What is the best and practice about this problems?

Upvotes: 1

Views: 2341

Answers (1)

Luan Castro
Luan Castro

Reputation: 1184

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

Upvotes: 5

Related Questions