简体   繁体   中英

How to find out the name of the root element?

Suppose I have downloaded a valid XML document via Ajax (var docum = request.responseXML;). Now how do I find out the name of the root element?

docum.documentElement.tagName
var xmlDoc;
if (window.DOMParser) {
    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(dataString, "text/xml");
} else {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(dataString);
}
xmlDoc.documentElement; // is what you need

If you have a string with xml then this will give you the first tag

var xml = "<foo><bar>......";
var tag = /<(.*?)>/.exec(xml)[1];
alert(tag) // foo

Alternatively you just access the node and retrieves the tagName

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