| Author |
Iterating through XML elements
|
|
|
| Hi
I have problem iterating throung an element's children
The XML is as follows:
<?xml version="1.0
<MODEL type="Profitability">
<FORM>1</FORM>
<ABSPROFIT></ABSPROFIT>
<RELPROFIT>1</RELPROFIT>
<THRESHOLD>1.75</THRESHOLD>
<CAP>
<ROW rowno="1">
<RANGE>Less than 1.8 (threshold)</RANGE>
<MONTH>0</MONTH>
</ROW>
<ROW rowno="2">
<RANGE>1.8 - 2.2</RANGE>
<MONTH>0.5</MONTH>
</ROW>
<ROW rowno="3">
<RANGE>2.3 - 2.7</RANGE>
<MONTH>1</MONTH>
</ROW>
</CAP>
</MODEL>
I can get the values for all nodes under <MODEL>, using
Set node = model.selectSingleNode("THRESHOLD")
debug.print Val(node.nodeTypedValue)
but I can't seem to figure out how to iterate nodes inside <CAP> and get
the ROW's elements
Set node = model.selectSingleNode("CAP")
For Each node 2In node.childNodes
Set node3 = node2.selectSingleNode("RANGE")
debug.print node3.nodeTypedValue
Next
Please help me, I've spending hours on this.
Thanks
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
| |
| Mike D Sutton 2005-02-26, 8:55 pm |
| > I have problem iterating throung an element's children
>
> The XML is as follows:
<snip>
> I can get the values for all nodes under <MODEL>, using
> Set node = model.selectSingleNode("THRESHOLD")
> debug.print Val(node.nodeTypedValue)
> but I can't seem to figure out how to iterate nodes inside <CAP> and get
> the ROW's elements
>
> Set node = model.selectSingleNode("CAP")
>
> For Each node 2In node.childNodes
> Set node3 = node2.selectSingleNode("RANGE")
> debug.print node3.nodeTypedValue
> Next
You can simply use an XPath query to select all the nodes at that level:
'***
Dim MyDOM As DOMDocument
Dim RowNodes As IXMLDOMNodeList
Dim RowNode As IXMLDOMNode
Set MyDOM = New DOMDocument
MyDOM.async = False
If (MyDOM.loadXML(Clipboard.GetText())) Then
Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW")
If (Not (RowNodes Is Nothing)) Then
For Each RowNode In RowNodes
Debug.Print RowNode.Attributes.getNamedItem("rowno").Text
Next RowNode
Set RowNodes = Nothing
End If
End If
Set MyDOM = Nothing
'***
(Assumes you have your demo XML copied to the clipboard (with the xml processing instruction complete ;))
Here's an old post with some more advanced XPath in case you want to drill further down your document:
http://groups-beta.google.com/group...4d31618fd8b3001
And some more information on using XML in VB in general:
http://groups-beta.google.com/group...c8f73ad9971fd89
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
| |
|
| Thanks for your reply. I have made the modifications to my coding according
to your sample and it works flawlessly.
What if the number of CAP element is more than 1. And each CAP has 1 or more
ROW elements. How to iterate through every ROWs in every CAPs displaying
their RANGE & MONTH values.
Sorry for all this questions since I'm a newbie in using XML DOM.
"Mike D Sutton" <EDais@mvps.org> wrote in message
news:uEhE8gEHFHA.2456@TK2MSFTNGP09.phx.gbl...
> <snip>
>
> You can simply use an XPath query to select all the nodes at that level:
>
> '***
> Dim MyDOM As DOMDocument
> Dim RowNodes As IXMLDOMNodeList
> Dim RowNode As IXMLDOMNode
>
> Set MyDOM = New DOMDocument
>
> MyDOM.async = False
> If (MyDOM.loadXML(Clipboard.GetText())) Then
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW")
>
> If (Not (RowNodes Is Nothing)) Then
> For Each RowNode In RowNodes
> Debug.Print RowNode.Attributes.getNamedItem("rowno").Text
> Next RowNode
>
> Set RowNodes = Nothing
> End If
> End If
>
> Set MyDOM = Nothing
> '***
>
> (Assumes you have your demo XML copied to the clipboard (with the xml
> processing instruction complete ;))
> Here's an old post with some more advanced XPath in case you want to drill
> further down your document:
> http://groups-beta.google.com/group...4d31618fd8b3001
> And some more information on using XML in VB in general:
> http://groups-beta.google.com/group...c8f73ad9971fd89
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>
>
| |
| Mike D Sutton 2005-02-27, 8:55 am |
| > Thanks for your reply. I have made the modifications to my coding according
> to your sample and it works flawlessly.
>
> What if the number of CAP element is more than 1. And each CAP has 1 or more
> ROW elements. How to iterate through every ROWs in every CAPs displaying
> their RANGE & MONTH values.
>
> Sorry for all this questions since I'm a newbie in using XML DOM.
Try it, the code I provided in my prior post copes with multiple <CAP> tags with multiple <ROW> child tags.
If you specifically want to find a particular child tag such as the <RANGE> tags then you can adjust your XPath query to
return only those:
'*** Select all range nodes
Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW/RANGE")
'***
Alternatively if you want to find a specific row then you can use this:
'*** Select any row nodes with a row number of 2
Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[@rowno=""2""]")
'***
If you want to select a specific <ROW> tag where one of it's child nodes is used to select it you can use something like
this:
'*** Select any row nodes with a month of 1
Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[MONTH=""1""]")
'***
Or if you wanted to return the entire <CAP> tag this specific month resides in:
'*** Select the cap nodes with a row node which has a month of 1
Set RowNodes = MyDOM.selectNodes("MODEL/CAP[ROW/MONTH=""1""]")
'***
If you are sure that the values within a specific tag are numeric only (usually this is ensured via a DTD) then you can
even perform simple mathematical queries:
'*** Select all row nodes with a month of less than 1
Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[MONTH<""1""]")
'***
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
| |
|
| Thank you for your time. This is interesting. I'll try playing with this a
lot more...
"Mike D Sutton" <EDais@mvps.org> wrote in message
news:u2w2t7LHFHA.580@TK2MSFTNGP15.phx.gbl...
>
> Try it, the code I provided in my prior post copes with multiple <CAP>
> tags with multiple <ROW> child tags.
> If you specifically want to find a particular child tag such as the
> <RANGE> tags then you can adjust your XPath query to
> return only those:
>
> '*** Select all range nodes
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW/RANGE")
> '***
>
> Alternatively if you want to find a specific row then you can use this:
>
> '*** Select any row nodes with a row number of 2
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[@rowno=""2""]")
> '***
>
> If you want to select a specific <ROW> tag where one of it's child nodes
> is used to select it you can use something like
> this:
>
> '*** Select any row nodes with a month of 1
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[MONTH=""1""]")
> '***
>
> Or if you wanted to return the entire <CAP> tag this specific month
> resides in:
>
> '*** Select the cap nodes with a row node which has a month of 1
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP[ROW/MONTH=""1""]")
> '***
>
> If you are sure that the values within a specific tag are numeric only
> (usually this is ensured via a DTD) then you can
> even perform simple mathematical queries:
>
> '*** Select all row nodes with a month of less than 1
> Set RowNodes = MyDOM.selectNodes("MODEL/CAP/ROW[MONTH<""1""]")
> '***
>
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>
>
|
|
|
|