Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Child nodes? 1

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Nov 21, 2003
4,772
US
I have a table that looks something like this:

Code:
<Orders>
<Order>
<Lines>
<Quantity>1</Quantity>
<SKU>12345</SKU>
<UnitCost>10.00</UnitCost>
</Lines>
<Lines>
<Quantity>2</Quantity>
<SKU>23456</SKU>
<UnitCost>3.00</UnitCost>
</Lines>
<CustomerID>12345</CustomerID>
<BillToName>John Smith</BillTOName>
... and so forth.

SO, there may be any number of "<Lines>" in the order, then followed by bill to, ship to, etc. information.

I'm parsing the XML all right, but I'm having a problem selecting the Lines tags for a specific parent (using an index).....

I *THINK* that what I need is childNode or something along those lines.... so I essentially have a set of nested For/Next loops, something like:
Code:
Set objRoot = xmlDoc.documentElement
Set oCustomer = xmlDoc.getElementsByTagName("Customer")
iNumCustomers = oCustomer.Length - 1
For i = 0 to iNumCustomers ' Loop through and do each of them separately

.... do a bunch of stuff to get the customer information using GetElementsByTagName("Whatever").item(i).text

Next

... HOWEVER, before my Next, I need to then loop through the "Lines" elements and fetch all the lines (in a nested loop, obviously)... but my question is:

How do I find out how many Lines tags there are in my current item (to set another loop, for j = 0 to ??? ) and then, how do I get the information for each of them (using GetElementsByTagName.item(j).text or something similar?)

TIA!



Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
[0] There are CustomerID tag, but none Customer.

[1] Modulo all typos and approximation in the question, this.
[tt] oCustomer.ParentNode.GetElementsByTagName("Lines").Length[/tt]
 
[1.1] The oCustomer object I meant each item of the collection. Since oCustomer is used in the place of the collection in your script. I have to amend it to look like this, for certain index i.
[tt] oCustomer[red](i)[/red].ParentNode.GetElementsByTagName("Lines").Length[/tt]
 
  • Thread starter
  • Moderator
  • #4
OK, I did a very simple test script against my XML file of:

Code:
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.ValidateOnParse = False

If xmlDoc.Load("E:\OrderOutput\Orders2010-07-27.xml") Then
    Set objRoot = xmlDoc.documentElement
	Set Orders = xmlDoc.getElementsByTagName("Order")


Wscript.Echo Orders.length

WScript.Echo Orders(0).ParentNode.GetElementsByTagName("Lines").Length

End If

Set xmlDoc = Nothing

.... and that returned the correct count of lines... so I can start my inner loop.... now how do I reference each of the fields? I'm assuming it's going to be another item() of some sort? I'll keep playing with it... but it's almost there. :)

Thanks in advance!


Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
  • Thread starter
  • Moderator
  • #5
Got it. This worked (for 3 lines)....

Code:
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.ValidateOnParse = False

If xmlDoc.Load("E:\OrderOutput\Orders2010-07-27.xml") Then
    Set objRoot = xmlDoc.documentElement
	Set Orders = xmlDoc.getElementsByTagName("Order")


Wscript.Echo Orders.length

' Set LinesRoot = xmlDoc.documentElement.getElementsByTagName("Lines").item(0).firstChild

WScript.Echo Orders(0).ParentNode.GetElementsByTagName("Lines").Length

WScript.Echo Orders(0).ParentNode.GetElementsByTagName("Quantity").item(0).text

WScript.Echo Orders(0).ParentNode.GetElementsByTagName("Quantity").item(1).text

WScript.Echo Orders(0).ParentNode.GetElementsByTagName("Quantity").item(2).text


End If

Set xmlDoc = Nothing

... That gives me enough to work with. Thanks again!


Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top