Extracting Values in Nodes and Child Nodes in XML

Julesdude

Board Regular
Joined
Jan 24, 2010
Messages
197
Hi there,

I am using a web service for the first time and need some help extracting the values returned in the XML after the POST.
I have a loop which does this successfully once I specify the parent node. But within the XML returned there is a point in which another parent node with child items.

What I want the loop to do is extract the values at both levels, so that when it hits another parent with child nodes, it is able to loop through these too and extract the values of them also line by line as it has been doing at the top level. I can't figure out how to do this though. Code so far that extracts values for the first level:

Code:
Dim Request As New MSXML2.XMLHTTP60
Dim Doc As New DOMDocument60
Dim xresult As MSXML2.IXMLDOMNode
Dim xentry As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim list As IXMLDOMNodeList
Dim r As Long
Dim sendXML As String


sendXML = "some send XML here"
    
    With Request
        .Open "POST", "[URL="http://vmrodshel107.oracleoutsourcing.com:45911/SLFDASUtilsWS/resources/DasUtils/getItems"]url here[/URL]", False, "username", "password"

        .setRequestHeader "Content-Type", "text/xml"
        .send sendXML

        Doc.LoadXML .responseText
            
    End With

    Dim i As Integer
    Set list = Doc.SelectNodes("//slf_item_ws_obj/item_ws_table/slf_item_ws_row")
    Dim node As IXMLDOMNode, nd As IXMLDOMNode
    Dim childNode As IXMLDOMNode
    i = 4
    For Each node In list
        i = i + 1
            MsgBox GetNodeValue(node, "item")
            MsgBox GetNodeValue(node, "item_status")
            MsgBox GetNodeValue(node, "slf_item_ws_row/item_level")
            MsgBox GetNodeValue(node, "slf_item_ws_row/transaction_level")
            MsgBox GetNodeValue(node, "slf_item_ws_row/concession_ind")

    Next node
Set Doc = Nothing
End Sub
Function GetNodeValue(node As IXMLDOMNode, xp As String)
    Dim n As IXMLDOMNode, nv
    Set n = node.SelectSingleNode(xp)
    If Not n Is Nothing Then nv = n.nodeTypedValue
    GetNodeValue = nv
End Function


here's an example of the XML that would be read:

Code:
<slf_item_ws_obj>
   <item_ws_table>
      <slf_item_ws_row>
         <error_message/>
         <item>10150459</item>
         <item_status>A</item_status>
         <brand_name>A BRAND NAME</brand_name>
         <child_items_ws_table>
            <slf_child_items_ws_row>
               <item>10150467</item>
               <item_desc>RG:MULTI 2:1SIZE</item_desc>
               <diff_1>MULTI2</diff_1>
               <diff_1_desc>MULTI 2</diff_1_desc>
               <diff_2>1SIZE</diff_2>
               <diff_2_desc>1SIZE</diff_2_desc>
            </slf_child_items_ws_row>
         </child_items_ws_table>
      </slf_item_ws_row>

I would want to get every value out of the nodes. They should be :

item: 101
2item: 102
item:10150467
item: 10150467

With the code above though I'm getting

item: 101


and then blanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Sorry the XML didn't appear correctly.

here's an example of the XML that would be read:
Code:
<slf_item_ws_obj><item_ws_table>(slf_item_ws_obj)
   (item_ws_table)
      (slf_item_ws_row)
         (error_message/)
         (item)10150459(/item)
         (item_status)A(/item_status)
         (item_level)1(/item_level)
         (brand_name)BRAND NAME(/brand_name)
         (child_items_ws_table)
            (slf_child_items_ws_row)
               (item)10150467(/item)
               (item_desc)YES:MULTI 2:1SIZE(/item_desc)
               (diff_1)MULTI2(/diff_1)
            (/slf_child_items_ws_row)
         (/child_items_ws_table)
      (/slf_item_ws_row)

I would want to get every value out of the nodes and child nodes preferably in sequence. Bear in mind that this chunk of XML would also be repeated times however many items there are so I would need to loop through each chunk and obtain each value in each child node. Something like this :




item: 101
item status: A
item_level: 1
brand_name: BRAND NAME
(child_items_ws_table)
(slf_child_items_ws_row)
item: 10150467
etc etc

With the code above though I'm getting only the first value and then blanks</item_ws_table></slf_item_ws_obj>
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,281
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top