解析 JavaScript 中的 Childnode 值

huangapple go评论60阅读模式
英文:

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

&lt;root&gt;&lt;example&gt;&lt;examplevalue&gt;exampletext&lt;/examplevalue&gt;&lt;examplevalue&gt;exampletext2&lt;/examplevalue&lt;/example&gt;&lt;/root&gt;

and I have the following javscript code

			if (window.DOMParser){ // Standard browsers
				var parser = new DOMParser();
				xmlDoc = parser.parseFromString(xmlString, &quot;text/xml&quot;);
			}
			else { // Internet Explorer
				xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
				xmlDoc.async = false;
				xmlDoc.loadXML(xmlString); 
			}
			var coll=xmlDoc.getElementsByTagName(&quot;example&quot;);
			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) =&gt; node.nodeValue || node.textContent;

 if (window.DOMParser){ // Standard browsers
  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(xmlString, &quot;text/xml&quot;);
}
else { // Internet Explorer
  xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
  xmlDoc.async = false;
  xmlDoc.loadXML(xmlString); 
}
var coll=xmlDoc.getElementsByTagName(&quot;example&quot;);
console.log(getNodeValue(coll[0].childNodes[0]))
// output =&gt; examplecontent

答案2

得分: 0

你需要使用childNodes属性导航到它,以访问第一个&lt;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 &lt;examplevalue> element then access its textContent property

if (window.DOMParser) { // Standard browsers
  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(xmlString, &quot;text/xml&quot;);
} else { // Internet Explorer
  xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
  xmlDoc.async = false;
  xmlDoc.loadXML(xmlString); 
}

var exampleValues = xmlDoc.getElementsByTagName(&quot;examplevalue&quot;);
console.log(exampleValues[0].textContent);

huangapple
  • 本文由 发表于 2023年2月23日 20:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定