If the file structure is as follows

<orders>
<orderlist>
<orderNum>1</orderNum>
<BillingAddress>
<name>John Smith</name>
<street>123 Street</street>
</BillingAddress>
</orderlist>
<orderlist>
<orderNum>2</orderNum>
<BillingAddress>
<name>Jane Smith</name>
<street>124 Street</street>
</BillingAddress>
<invoice>
<lineitemlist>
<PartNumber>123</PartNumber>
<QtySold>5</QtySold>
</lineitemlist>
<lineitemlist>
<PartNumber>654</PartNumber>
<QtySold>2</QtySold>
</lineitemlist>
</invoice>
</orderlist>
</orders>

I'm trying to loop through each lineitem for each order. I've tried using the same structure but it doesn't seem to work. I'm pulling all of the Lineitem lists for the whole document.


Option Explicit
Dim strFileName
Dim objDoc, objNodeList, objNode, objSubNode
Dim OrderNum, Email, BFirst, Blast, BAddress, BCity, BState, BZip, BCountry
Dim SFirst, Slast, SAddress, SCity, SState, SZip, SCountry
Dim PartNumber, Qty, PartNodeList, objPartNode, objPart

strFileName = "C:\Inetpub\wwwroot\XML\XMLTesting\NSAPI-10-30-12-51.xml"

'Create the document object and load the xml
Set objDoc = CreateObject("Microsoft.XMLDOM")
objDoc.Async = False
objDoc.Load strFileName

'Get all the orderlist nodes
Set objNodeList = objDoc.SelectNodes("//OrderList")

wscript.echo objNodeList.length & " Order(s) in file"

'Loop through each of the returned nodes in the nodelist
For Each objNode In objNodeList
'Intialize the return variables
wscript.echo"-----------------------------"
'Get the order number
Set objSubNode = objNode.SelectSingleNode("orderNum")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
OrderNum = objSubNode.Text
End If

'Get the Email node
Set objSubNode = objNode.SelectSingleNode("Customer/EmailAddress")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
Email = objSubNode.Text
wscript.echo Email
End If

Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/FirstName")
If Not objSubNode Is Nothing Then
BFirst = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/LastName")
If Not objSubNode Is Nothing Then
BLast = objSubNode.Text
End If

'Get the street node
Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/Address1")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
BAddress = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/City")
If Not objSubNode Is Nothing Then
BCity = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/StateProvince")
If Not objSubNode Is Nothing Then
BState = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/BillingAddress/PostalCode")
If Not objSubNode Is Nothing Then
BZip = objSubNode.Text
End If

' wscript.echo "BILLING"
' wscript.echo "---------"
' wscript.echo BFirst &" "& BLast
' wscript.echo BAddress
' wscript.echo BCity &" "& BState &" "&BZip

Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/FirstName")
If Not objSubNode Is Nothing Then
SFirst = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/LastName")
If Not objSubNode Is Nothing Then
SLast = objSubNode.Text
End If

'Get the street node
Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/Address1")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
SAddress = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/City")
If Not objSubNode Is Nothing Then
SCity = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/StateProvince")
If Not objSubNode Is Nothing Then
SState = objSubNode.Text
End If

Set objSubNode = objNode.SelectSingleNode("Customer/ShippingAddress/PostalCode")
If Not objSubNode Is Nothing Then
SZip = objSubNode.Text
End If

' wscript.echo "SHIPPING"
' wscript.echo "---------"
' wscript.echo SFirst &" "& SLast
' wscript.echo SAddress
' wscript.echo SCity &" "& SState &" "&SZip
'
' wscript.echo "Order Items"
' wscript.echo "---------"

Set PartNodeList = objNode.SelectNodes("//OrderList/Invoice/LineItemList")

For Each objPartNode In PartNodeList
Set objPart = objPartNode.SelectSingleNode("PartNumber")
If Not objPartNode Is Nothing Then
PartNumber = objPartNode.Text
End If

Set objPart = objPartNode.SelectSingleNode("QtySold")
If Not objPartNode Is Nothing Then
Qty = objPartNode.Text
End If

wscript.echo Qty
wscript.echo "---------"

Next
wscript.echo "-------------------------"
Next