|
-
Oct 28th, 2010, 11:45 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Access childnodes XMLDOM
I'm attempting to access some childnodes but am having trouble. I can get the first child nodes but not the child nodes inside the child nodes
<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>
</orderlist>
</orders>
I'm able grab the ordernumbers for each order but I need to grab everything else for that particular order. I've google and google and googled for a method but am unsuccessful so far.
Here is my method:
Set Orders=objXMLDoc.getElementsByTagName("OrderList")
If Orders.length > 0 Then
wscript.echo Orders.length & " Order(s) in file"
wscript.echo "*****************************"
For Each y In Orders
LineNum=1
For Each y In Orders
LineNum=1
For Each oChildNode in y.ChildNodes
If oChildNode.nodeName="OrderNum" Then
OrderNum = oChildNode.text
End If
'ANY THING I'vE TRIED HERE TO GIVE ME THE BILLING INFO HAS GIVEN ME ALL OF THE BILLING INFO FOR EACH ORDER.
Next
Next
How would I get the billing info for just each order as I go through the file? I'm using VBS with CreateObject("Microsoft.XMLDOM")
Last edited by Dubya007; Oct 28th, 2010 at 11:54 PM.
"...Men will still say THIS was our finest hour"
If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?
-
Oct 30th, 2010, 08:24 AM
#2
Re: Access childnodes XMLDOM
Assuming the file structure is as posted then this should get you started.
Code:
Option Explicit
Dim strFileName
Dim objDoc, objNodeList, objNode, objSubNode
Dim strOrderNum, strName, strStreet, strResults
strFileName = "C:\Documents and Settings\Mark\Desktop\Orders sample\orders.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
strOrderNum = ""
strName = ""
strStreet = ""
'Get the order number
Set objSubNode = objNode.SelectSingleNode("orderNum")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strOrderNum = objSubNode.Text
End If
'Get the name node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/name")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strName = objSubNode.Text
End If
'Get the street node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/street")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strStreet = objSubNode.Text
End If
strResults = "Order Number: " & strOrderNum & vbCrLf & _
"Name: " & strName & vbCrlf & "Street: " & strStreet
wscript.echo strResults
Next
'Cleanup
Set objNodeList = Nothing
Set objDoc = Nothing
-
Oct 30th, 2010, 11:13 AM
#3
Thread Starter
Fanatic Member
Re: Access childnodes XMLDOM
That worked beautifully. Thank you so much you've saved me hours of frustration.
"...Men will still say THIS was our finest hour"
If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?
-
Nov 1st, 2010, 08:50 AM
#4
Thread Starter
Fanatic Member
Re: Access childnodes XMLDOM
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
"...Men will still say THIS was our finest hour"
If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?
-
Nov 1st, 2010, 09:49 AM
#5
Re: Access childnodes XMLDOM
The stuff highlighted in red was added to my original post.
Code:
Option Explicit
Dim strFileName
Dim objDoc, objNodeList, objNode, objSubNode, objLineItemNodes, objLineItemNode
Dim strOrderNum, strName, strStreet, strResults
strFileName = "C:\Documents and Settings\Mark\Desktop\Orders sample\orders.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
strOrderNum = ""
strName = ""
strStreet = ""
'Get the order number
Set objSubNode = objNode.SelectSingleNode("orderNum")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strOrderNum = objSubNode.Text
End If
'Get the name node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/name")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strName = objSubNode.Text
End If
'Get the street node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/street")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strStreet = objSubNode.Text
End If
strResults = "Order Number: " & strOrderNum & vbCrLf & _
"Name: " & strName & vbCrlf & "Street: " & strStreet
Set objSubNode = objNode.SelectSingleNode("invoice")
If Not objSubNode Is Nothing Then
Set objLineItemNodes = objSubNode.SelectNodes("lineitemlist")
For Each objLineItemNode in objLineItemNodes
Set objSubNode = objLineItemNode.SelectSingleNode("PartNumber")
If Not objSubNode Is Nothing Then
strResults = strResults & vbCrLf & " Part Number: " & objSubNode.Text
End If
Set objSubNode = objLineItemNode.SelectSingleNode("QtySold")
If Not objSubNode Is Nothing Then
strResults = strResults & vbCrLf & " Qty Sold: " & objSubNode.Text
End If
Next
End If
wscript.echo strResults
Next
'Cleanup
'Set = Nothing
Set objNodeList = Nothing
Set objDoc = Nothing
-
Nov 1st, 2010, 10:36 AM
#6
Thread Starter
Fanatic Member
Re: Access childnodes XMLDOM
Wow I was actually working towards that while I waited. Thank you so much.
What if the <Orderlist> tag has an attribute of type? is it possible to access that info?
<orderlist type="resident">
<orderlist type="commercial">
I've looked at some examples on the internet and this what I came up with. It isn't working.
vb Code:
Set objSubNode = objNode.SelectSingleNode("OrderList")
If Not objSubNode Is Nothing Then
OrderType = objSubNode.Attributes.getNamedItem("type").Text
wscript.echo OrderType
End If
"...Men will still say THIS was our finest hour"
If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?
-
Nov 1st, 2010, 10:53 AM
#7
Re: Access childnodes XMLDOM
Again the changes have been highlighted
Code:
Option Explicit
Dim strFileName
Dim objDoc, objNodeList, objNode, objSubNode, objLineItemNodes, objLineItemNode, objAt
Dim strOrderNum, strName, strStreet, strResults, strAt
strFileName = "C:\Documents and Settings\Mark\Desktop\Orders sample\orders.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
strOrderNum = ""
strName = ""
strStreet = ""
strAt = ""
Set objAt = objNode.Attributes.GetNamedItem("type")
If Not objAt Is Nothing Then
strAt = "Type: " & objAt.Text & vbCrLf
End If
'Get the order number
Set objSubNode = objNode.SelectSingleNode("orderNum")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strOrderNum = objSubNode.Text
End If
'Get the name node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/name")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strName = objSubNode.Text
End If
'Get the street node
Set objSubNode = objNode.SelectSingleNode("BillingAddress/street")
'Make sure a node was returned
If Not objSubNode Is Nothing Then
strStreet = objSubNode.Text
End If
strResults = "Order Number: " & strOrderNum & vbCrLf & _
"Name: " & strName & vbCrlf & "Street: " & strStreet
strResults = strAt & strResults
Set objSubNode = objNode.SelectSingleNode("invoice")
If Not objSubNode Is Nothing Then
Set objLineItemNodes = objSubNode.SelectNodes("lineitemlist")
For Each objLineItemNode in objLineItemNodes
Set objSubNode = objLineItemNode.SelectSingleNode("PartNumber")
If Not objSubNode Is Nothing Then
strResults = strResults & vbCrLf & " Part Number: " & objSubNode.Text
End If
Set objSubNode = objLineItemNode.SelectSingleNode("QtySold")
If Not objSubNode Is Nothing Then
strResults = strResults & vbCrLf & " Qty Sold: " & objSubNode.Text
End If
Next
End If
wscript.echo strResults
Next
'Cleanup
'Set = Nothing
Set objNodeList = Nothing
Set objDoc = Nothing
And the xml looks like this
Code:
<orders>
<orderlist type="resident">
<orderNum>1</orderNum>
<BillingAddress>
<name>John Smith</name>
<street>123 Street</street>
</BillingAddress>
</orderlist>
<orderlist type="commercial">
<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>
-
Nov 1st, 2010, 10:58 AM
#8
Thread Starter
Fanatic Member
Re: Access childnodes XMLDOM
That worked perfectly. Thank you so much for all of your help. 
"...Men will still say THIS was our finest hour"
If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|