Solved

Getting a list of Nodes by tag name, starting at a particular Node in the tree...

Posted on 2006-11-14
7
272 Views
Last Modified: 2008-02-01
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.getElementsByTagName("item")[1];

Can I do something like widget2.getElementsByTagName("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?
0
Comment
Question by:Melvinivitch
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17937175
xmlDoc.SelectNodes("store/item[@iName = 'widget2']/repeat");

This will return an XmlNodeList with all of the repeat nodes under the item with the iName of widget2.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17937238
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/item[@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].nodeValue);
                                  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
0
 

Author Comment

by:Melvinivitch
ID: 17937371
Thanks...

When I use that code: var itemNode=oXML.selectNodes("/store/item[@iName = 'widget2']/repeat");
where oXML is my toplevel xmlDoc object,
and then do: itemNode.childNodes[0].nodeName;
IE gives me this message: "childNodes.0 is null or not an object" ...

Thanks again...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17937514
I am guessing that this :

 test = test + (itemNode.childNodes[0].nodeValue);

should be :

 test = test + (itemNode.childNodes[j].nodeValue);
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 325 total points
ID: 17937538
no,
should be test = test + (itemNode[j].childNodes[0].nodeValue);

itemNode[j] is the jth <repeat> element (the jth element in your nodelist)

remeber from one of your previous questions:
childNodes[0].nodeValue is there to get at the text value

you are right, there was a bug in my code, but at a different locaton than you thought

To allign with your question
var itemNode=oXML.selectNodes("/store/item[@iName = 'widget2']/repeat");
should be
var itemNode=xmlDoc.selectNodes("/store/item[@iName = 'widget2']/repeat");

cheers

Geert


0
 

Author Comment

by:Melvinivitch
ID: 17940441
Thanks again.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17940532
welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now