英文:
Parsing Childnode values in javascript
问题
XML:
<root><example><examplevalue>exampletext</examplevalue><examplevalue>exampletext2</examplevalue></example></root>
JavaScript 代码:
if (window.DOMParser) { // 标准浏览器
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
} else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var coll = xmlDoc.getElementsByTagName("example");
console.log(coll[0].childNodes[0].nodeValue);
控制台输出:
null
我期望的输出:
exampletext
为什么子节点没有被正确解析?
childNodes
似乎是节点类的一个方法。但 getElementByName
返回一个 HTMLCollection 对象。这是如何工作的?
感谢任何提示。
英文:
I have an XML
<root><example><examplevalue>exampletext</examplevalue><examplevalue>exampletext2</examplevalue</example></root>
and I have the following javscript code
if (window.DOMParser){ // Standard browsers
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
}
else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var coll=xmlDoc.getElementsByTagName("example");
console.log(coll[0].childNodes[0].nodeValue);
The console output is
null
I would expect it to be
exampletext
Why are the childnodes not parsed correctly?
childNodes seems to be a method of class node. But the getElementByName return a HTMLCollection object. How does that work?
Thanks for any hints.
答案1
得分: 1
因为您进入了window.DOMParser
条件。对于DOMParser
,您需要使用textContent
。
在您的情况下,您可以创建一个名为getNodeValue
的函数,并传递节点。然后获取可用的内容。
const getNodeValue = (node) => node.nodeValue || node.textContent;
if (window.DOMParser){ // 标准浏览器
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
}
else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var coll=xmlDoc.getElementsByTagName("example");
console.log(getNodeValue(coll[0].childNodes[0]))
// 输出 => examplecontent
英文:
It because you fell into window.DOMParser
condition. For DOMParser
you need to use textContent
.
In your case, you can create a function called getNodeValue
and pass the node. Then get which one is available.
const getNodeValue = (node) => node.nodeValue || node.textContent;
if (window.DOMParser){ // Standard browsers
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
}
else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var coll=xmlDoc.getElementsByTagName("example");
console.log(getNodeValue(coll[0].childNodes[0]))
// output => examplecontent
答案2
得分: 0
你需要使用childNodes
属性导航到它,以访问第一个<examplevalue
>元素的值,然后访问它的textContent
属性
if (window.DOMParser) { // 标准浏览器
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
} else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var exampleValues = xmlDoc.getElementsByTagName("examplevalue");
console.log(exampleValues[0].textContent);
英文:
you need to navigate to it using the childNodes
property to access the value of the first <examplevalue
> element then access its textContent
property
if (window.DOMParser) { // Standard browsers
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
} else { // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
var exampleValues = xmlDoc.getElementsByTagName("examplevalue");
console.log(exampleValues[0].textContent);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论