RDK
RDK

Reputation: 385

Processing XML in ASP pages

Folks....Probably a simple question, but I have not been able to find the simple answer. Assume the following XML structure is being analyzed in an ASP or ASPx page.

<level1>
<Block1>
 <codes>
    <a>XS</a>
    <b>SM</b>
    <c>M</c>
    <d>L</d>
    <e>XL</e>
 </codes>
</Block1>
<Block2>
 <codes>
    <a>XP</a>
    <b>P</b>
    <c>M</c>
    <d>G</d>
    <e>XG</e>
 </codes>
</Block2>
</level1>

Now, I can and I have used VBS code like For each L2Element in L1.Childnodes... to iterate through the levels, blocks and the codes. However, if I really only interested in getting the text for the tag <c> in <Block2>, Is there a simple method which I can use to get it directly without iterating through all levels, blocks and codes?

Thanks....RDK

Upvotes: 0

Views: 124

Answers (1)

crush
crush

Reputation: 17023

You can use XPath to query the node you want.

Dim xmlDoc : Set xmlDoc = [your xml document here]
xmlDoc.setProperty "SelectionLanguage", "XPath"

Then, you can select the node you want as follows:

Dim oNode    
Set oNode = xmlDoc.selectSingleNode("//Block2/codes/c")

You can then retrieve the value from oNode. Don't forget to check if it is Nothing first, though. oNode will be equal to Nothing if the XPath query didn't find it's target.

In the event that you want to select multiple nodes, there is also a selectNodes() method with which you can also use XPath. It will return a collection of nodes, that has a length property that you can use for iterating through them. Access each node of the collection with item(index).

Upvotes: 1

Related Questions