开发者

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 t开发者_如何学编程he 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜