按名称访问XML DOM子节点 [英] Accessing XML DOM child nodes by name

查看:108
本文介绍了按名称访问XML DOM子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript中访问XML对象的textContent属性。根项目有几个孩子自己也有一些孩子。为了让孩子在第一级,我只是遍历根元素的childNodes数组。但是要获取孙子的值,我想使用类似 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



http://jsfiddle.net/GQ8Kd/

  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.

http://jsfiddle.net/GQ8Kd/

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆