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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…