Javascript XML解析器不支持';行不通
我知道在互联网上有很多关于这个问题的问题和文章,但不知怎么的,我无法让它发挥作用。我很确定我遗漏了一些基本的东西,但我找不到 解析本身:Javascript XML解析器不支持';行不通,javascript,xml,parsing,dom,Javascript,Xml,Parsing,Dom,我知道在互联网上有很多关于这个问题的问题和文章,但不知怎么的,我无法让它发挥作用。我很确定我遗漏了一些基本的东西,但我找不到 解析本身: var str="<article>Some article</article><other>Other stuff</other>"; var xmlDoc = null; if (window.DOMParser) { var parser = new DOMParser();
var str="<article>Some article</article><other>Other stuff</other>";
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(str,"text/xml");
}
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject ("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(str);
}
var node = xmlDoc.getElementsByTagName("article")[0].childNodes[0].nodeValue;
alert (node);
此外,如果我像这样使用str,它也可以工作:
var str="<article>Some article</article>";
var str=“某篇文章”;
所以问题是,为什么它不起作用?即使我只在str变量的末尾附加一个字符,解析也不能正常工作。您还可以给我指出一些关于这种行为的有用教程吗?您的字符串不是有效的XML,因为它有多个根节点。你是说:
<article><name>Some article</name><other>Something else</other></article>
一些文章或其他东西
您的字符串不是有效的XML,因为它有多个根节点。你是说:
<article><name>Some article</name><other>Something else</other></article>
一些文章或其他东西
尝试使用
var str="<root><article>Some article</article><other>Other stuff</other></root>";
var node = xmlDoc.documentElement.getElementsByTagName("article")[0].childNodes[0].nodeValue;
documentElement property returns the root tag of an xml document , once you get the root tag, next you can extract elements by tag name , child nodes ....
var str=“Some articleOther stuff”;
var node=xmlDoc.documentElement.getElementsByTagName(“文章”)[0].childNodes[0].nodeValue;
documentElement属性返回xml文档的根标记,一旦获得根标记,接下来可以按标记名、子节点提取元素。。。。
尝试使用
var str="<root><article>Some article</article><other>Other stuff</other></root>";
var node = xmlDoc.documentElement.getElementsByTagName("article")[0].childNodes[0].nodeValue;
documentElement property returns the root tag of an xml document , once you get the root tag, next you can extract elements by tag name , child nodes ....
var str=“Some articleOther stuff”;
var node=xmlDoc.documentElement.getElementsByTagName(“文章”)[0].childNodes[0].nodeValue;
documentElement属性返回xml文档的根标记,一旦获得根标记,接下来可以按标记名、子节点提取元素。。。。
可能是因为它不是有效的XML结构…?可能是因为它不是有效的XML结构。。。?