简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM