无法从在Javascript中的NodeList检索子节点列表 [英] Unable to retrieve a sub-NodeList from a NodeList in Javascript

查看:94
本文介绍了无法从在Javascript中的NodeList检索子节点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的交易:我尝试从一个已经存在的节点列表的NodeList对象。这里有一个简单的XML例子:

So here's the deal: I'm trying to retrieve a NodeList object from an already existing NodeList. Here's a simplified XML example:

<products>
  <category>
  <name>Category A</name>
    <product>
      <code>1</code>
      <name>Product 1 Category A</name>
      <price>10.0</price>
    </product>
    <product>
      <code>2</code>
      <name>Product 2 Category A</name>
      <price>20.0</price>
    </product>
  </category>
  <category>
  <name>Category B</name>
    <product>
      <code>3</code>
      <name>Product 1 Category B</name>
      <price>5.0</price>
    </product>
    ...
  </category>
</products>

正如你所看到的,标签名称出现了两次,一次是作为类的子节点,再作为产品的一个子节点。我想只检索产品名称。既然不能从XML文件中读取,而是接受它作为一个字符串,这里是我的解析功能:

As you can see, the tag name appears twice, once as a child node of category and again as a child node of product. I wish to retrieve only product names. Since I can't read from the XML file but rather receiving it as a string, here's my parse function:

function parseXML(xmlString) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xmlString, "text/xml");
    var products = xmlDoc.getElementsByTagName("product");
    var names = products.tags("name"); //Here's my problem
    for(var i = 0; i < names.length; i++){
        var element = names[i];
        var name = element.firstChild;
        $('#div_products').append(name.data + "<br>");  
    }
    $('#div_main').html($('#div_products').html());
}       

这是我用什么作为参考: http://help.dottoro.com/ljtrjxbf。 PHP 。使用nodeListObject.tags(标签),但是,将产生以下错误:

This is what I'm using as reference: http://help.dottoro.com/ljtrjxbf.php. Using nodeListObject.tags("tag"), however, will produce the following error:

processMessage failed: Stack: TypeError: Object #<a NodeList> has no method 'tags'

我尝试不同的方法,但没有奏效。即使

I've trying different approaches, but nothing worked. Even

var names = products["name"];

返回未定义,这不会在任何情况下为我工作,因为文档中说,除了IE浏览器,它会只返回第一个节点,
A)我与Android /科尔多瓦和工作
B)有一个在节点没有属性名呢。

returns "undefined", which wouldn't work for me in any case, since the documentation says that aside from IE, it will return only the first node and A) I'm working with Android/Cordova and B) There's no attribute "name" in the node anyway.

那么,如何来解决这一问题?我以为我可以尝试建立从产品的新XMLDocument对象的NodeList但我还没看着它,因为它必须要解决这个问题更琐碎的方式。

So how do I work this out? I supposed I could try to create a new XMLDocument object from the products NodeList but I haven't looked into it since it must have a more trivial way to solve this problem.

推荐答案

感谢您Teemu,我已经成功地实现我想要的东西,在我的JavaScript code进行一些调整。我将它张贴在这里,这样也许有人可能会发现它在未来有帮助的:

Thank you Teemu, I've managed to achieve what I wanted with a few tweaks in my Javascript code. I'll post it here so that maybe someone might find it helpful in the future:

function parseXML(xmlString) {
    var NAME = 5; 
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xmlString, "text/xml");
    var products = xmlDoc.getElementsByTagName("product");

    for(var i = 0; i < products.length; i++){
        var nodeList = produtos[i].childNodes;
        $('#div_products').append(nodeList[NAME].textContent + "<br>");
    }

    $('#div_main').html($('#div_products').html());
} 

请注意,5是DOM TextNode我想要的指数(产品名称本身),因此:

Please notice that 5 is the index of the DOM TextNode I wanted (the product name itself), hence:

var NAME = 5;

这将是所有的。

这篇关于无法从在Javascript中的NodeList检索子节点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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