按名称访问XML DOM子节点 [英] Accessing XML DOM child nodes by name
问题描述
getElementsByTagName()
的东西,这不起作用。目前我只是循环遍历所有的孩子,并检查每个的nodeName属性来获取我的值。 XML(请注意:我内部获得的XML文档是未格式化的,没有空格,没有 #text 节点):
根>
< element>
< child1> content< / child1>
< child2> content< / child2>
< child3> content< / child3>
< / element>
< element>
< child1> content< / child1>
< child2> content< / child2>
< child3> content< / child3>
< / element>
< / root>
我到目前为止所尝试的
xmlDoc = xmlhttp.responseXML; (i = 0; i< xmlDoc.documentElement.childNodes.length; i ++)
{
key = xmlDoc.documentElement.childNodes [i];
alert(key.getElementsByTagName('child1')[0] .textContent);
}
这会导致一个消息框: undefined br>
和控制台错误: TypeError:key.getElementsByTagName(...)[0]未定义
浏览器:Firefox 26
也许这是DOM对象的问题,我以如下方式创建:
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
xmlDoc = xmlhttp.responseXML;
问题是节点之间的空格自动生成textNodes。检查 xmlDoc.documentElement.childNodes [i]
之间的节点是否为$ code> textNode (nodeType 3),然后再尝试找孩子我也删除了这个例子中的全局变量 i
和 key
。
var node,childNodes = xmlDoc.documentElement.childNodes;
for(var i = 0; i< childNodes.length; i ++)
{
node = childNodes [i];
if(node.nodeType!== Node.TEXT_NODE)console.log(node.getElementsByTagName('child1')[0] .textContent);
}
I want to access the textContent property of an XML object in JavaScript. The root item has several children which also have some children themselves. To get the children on the first level I just iterate through the childNodes Array of the root element. But to get the values of the "grandchilds" I would like to use something like getElementsByTagName()
, which doesn't work. Currently I just iterate through all children again and check the nodeName property of each to get my values. Is there a way to get the child object by name?
XML (Notice: the XML document I get internally is unformatted, there are no whitespaces, there are no #text nodes):
<root>
<element>
<child1>content</child1>
<child2>content</child2>
<child3>content</child3>
</element>
<element>
<child1>content</child1>
<child2>content</child2>
<child3>content</child3>
</element>
</root>
What I tried so far:
xmlDoc = xmlhttp.responseXML;
for(i = 0; i < xmlDoc.documentElement.childNodes.length; i++)
{
key = xmlDoc.documentElement.childNodes[i];
alert(key.getElementsByTagName('child1')[0].textContent);
}
which results in an message box: undefined
and an console error: TypeError: key.getElementsByTagName(...)[0] is undefined
Browser: Firefox 26
Maybe it's a problem with the DOM Object, I create it the following way:
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc = xmlhttp.responseXML;
The problem is the spaces between the nodes are automatically made textNodes. Check if the node at xmlDoc.documentElement.childNodes[i]
is a textNode
(nodeType 3) before you try to find children. I also removed your globals i
and key
in this example.
var node, childNodes = xmlDoc.documentElement.childNodes;
for(var i = 0; i < childNodes.length; i++)
{
node = childNodes[i];
if(node.nodeType !== Node.TEXT_NODE) console.log(node.getElementsByTagName('child1')[0].textContent);
}
这篇关于按名称访问XML DOM子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!