Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

android - Unable to retrieve a sub-NodeList from a NodeList in Javascript

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>

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());
}       

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"];

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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());
} 

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

var NAME = 5;

That would be all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...