Melvinivitch
asked on
Getting a list of Nodes by tag name, starting at a particular Node in the tree...
I'm new to XML. I want to get a list of Nodes by tag name, but starting at some node/depth in the tree - not starting at the top level.
For example, if I have the following simple XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
<item iName="widget1">
<title> Widget One </title>
<baseprice> 35.00 </baseprice>
<repeat> x1 </repeat>
<repeat> x2 </repeat>
<repeat> x3 </repeat>
<more> etc </more>
</item>
<item iName="widget2">
<title> Widget Two </title>
<baseprice> 1500.00 </baseprice>
<repeat> y1 </repeat>
<repeat> y2 </repeat>
<repeat> y3 </repeat>
<more> data </more>
</item>
<item iName="widget3">
<title> Widget Three </title>
<baseprice> 300.00 </baseprice>
</item>
</store>
and some javascript:
var widget2 = xmlDoc.getElementsByTagNam e("item")[ 1];
Can I do something like widget2.getElementsByTagNa me("repeat ") in order to get a list of all the repeat tags within the widget2 tag? In the docs, I don't see any "getElementsByTagName" or similar function in the Node class. Does it only exist at the top level? How do I get a list of tags starting at a particular Node in the tree?
For example, if I have the following simple XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
<item iName="widget1">
<title> Widget One </title>
<baseprice> 35.00 </baseprice>
<repeat> x1 </repeat>
<repeat> x2 </repeat>
<repeat> x3 </repeat>
<more> etc </more>
</item>
<item iName="widget2">
<title> Widget Two </title>
<baseprice> 1500.00 </baseprice>
<repeat> y1 </repeat>
<repeat> y2 </repeat>
<repeat> y3 </repeat>
<more> data </more>
</item>
<item iName="widget3">
<title> Widget Three </title>
<baseprice> 300.00 </baseprice>
</item>
</store>
and some javascript:
var widget2 = xmlDoc.getElementsByTagNam
Can I do something like widget2.getElementsByTagNa
Hi Melvinivitch,
Here is what you could do
First you can use SelectNodes to address the node you want to select with XPath
That is a lot more straightforward then using getElementsByTagName.
If you are sure you only want one node instead of a nodelist,
you can use selectSingleNode
You can learn about XPath here
http://www.w3schools.com/xpath/default.asp
var itemNode=oXML.selectNodes( "/store/it em[@iName = 'widget2']/repeat");
After that you can loop over the nodes like this
for (j=0; j<itemNode.length; j++)
{
test = test + (itemNode[j].nodeName);
test = test + ("=");
test = test + (itemNode.childNodes[0].no deValue);
test = test + ("<br />");
}
In this example I write the repeat values in a test string, of course you can use them in your response
The result will be like this
repeat = y1
repeat = y2
repeat = y3
Cheers!
Geert
Here is what you could do
First you can use SelectNodes to address the node you want to select with XPath
That is a lot more straightforward then using getElementsByTagName.
If you are sure you only want one node instead of a nodelist,
you can use selectSingleNode
You can learn about XPath here
http://www.w3schools.com/xpath/default.asp
var itemNode=oXML.selectNodes(
After that you can loop over the nodes like this
for (j=0; j<itemNode.length; j++)
{
test = test + (itemNode[j].nodeName);
test = test + ("=");
test = test + (itemNode.childNodes[0].no
test = test + ("<br />");
}
In this example I write the repeat values in a test string, of course you can use them in your response
The result will be like this
repeat = y1
repeat = y2
repeat = y3
Cheers!
Geert
ASKER
Thanks...
When I use that code: var itemNode=oXML.selectNodes( "/store/it em[@iName = 'widget2']/repeat");
where oXML is my toplevel xmlDoc object,
and then do: itemNode.childNodes[0].nod eName;
IE gives me this message: "childNodes.0 is null or not an object" ...
Thanks again...
When I use that code: var itemNode=oXML.selectNodes(
where oXML is my toplevel xmlDoc object,
and then do: itemNode.childNodes[0].nod
IE gives me this message: "childNodes.0 is null or not an object" ...
Thanks again...
I am guessing that this :
test = test + (itemNode.childNodes[0].no deValue);
should be :
test = test + (itemNode.childNodes[j].no deValue);
test = test + (itemNode.childNodes[0].no
should be :
test = test + (itemNode.childNodes[j].no
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again.
welcome
This will return an XmlNodeList with all of the repeat nodes under the item with the iName of widget2.