first time poster here and somewhat newbie to VBscript. I could really use some help from you guys who know this like second nature. I've tried to include some pertinent information and hopefully not too much.
I've been trying to get this to work and am finally reaching out after a few days time of attempts and a dozen iterations of code. I haven't found examples of extracting data from multiple levels (noes and chidlren) within the XML document.
I've been tasked with extracting data from an XML file using VBScript. the specific items are: Year, Account Number, Current Amount Due, Has Delinquent? (true/false) and Formatted Warrant Number.
The format of the XML file is as below, with anywhere from 1,000 to 10,000+ nodes filled with this data along with plenty of 'misc' nodes in there as well.
<BillData>
<BillHeader>
<Year>2010</Year>
<misc></misc>
<misc2></misc2>
<misc3></misc3>
<AcctNumber>0002566129</AcctNumber>
<misc4></misc4>
<PayAmounts>
<CurrentAmountDue>133.06</CurrentAmountDue>
<misc5></misc5>
</PayAmounts>
<misc6></misc6>
<HasDelinquents>true</HasDelinquents>
<WarrantInfo>
<FormattedWarrantNumber>201115447</FormattedWarrantNumber>
</WarrantInfo>
</BillHeader>
</BillData>
CurrentAmountDue and FormattedWarrantNumber may not always be present. by this I dont mean they are blank, but the entire entry of CurrentAmountDue may be missing, as shown below.
<PayAmounts>
<misc5></misc5>
</PayAmounts>
I need the extract this data to a comma separated text file. If the data is not present then I just need to insert the comman, so when the output is eventually imported to Excel it can be noted to be blank.
The challenge for me is to get into the different child nodes and extract the data correctly. I can't seem to select the different nodes correctly.
These are some links I've used as reference, but can't seem to get it working.
http://technet.microsoft.com/en-us/magazine/2007.02.heyscriptingguy.aspx this seemed to be the direction to go in, but I get an error "Node Test Expected Here":
Set colNodes=xmlDoc.SelectNodes("/BillData/BillHeader/*" (Year | Account | CurrentAmountDue)")
I found a post on Stack which suggested using this technique below, but it doesn't work for me once I get past two values, whereas I have more. I'm guessing this is due to the CurrentAmountDue and FormattedWarrantNumber are deeper levels into the XML so to speak.
strQuery = "/BillData/BillHeader/ " & _
"[name()='Year' or name()='AccountNumber' or name()='HasDelinquents' or name()='CurrentAmountDue' or name()='FormattedWarrantNumber']"
To my surprise, I am able to get this to return some values but not all on the same loop so my output is off (first line will only display year, last line is missing) and is just a comma.
strQuery = "/BillData/BillHeader/*"
Set colNodes=xmlDoc.selectNodes(strQuery)
For Each objNode in colNodes
' some lame if then statements that get the values, but this can't be the correct approach!
' these three items (Year, Account and HasDelinquents are under each BillHeader as far as I can tell, but this doesn't seem to be the most effective method.
if objNode.nodeName = "Year" then strYear = objNode.text
if objNode.nodeName = "Account" then strAccount = objNode.text
if objNode.nodeName = "HasDelinquents" then strHasDelq = objNode.text
for each CurrentAmt in objNode.SelectNodes("./CurrentAmountDue")
strCurrAmt = CurrentAmt.text
' i finally got a value here when I use msgbox to view it.'
next
for each WarrantNum in objNode.SelectNodes("./FormattedWarrantNumber")
strWarNum = WarrantNum.text
' getting this value also when I use msgbox to view it.
next
next
So you can see my attempts are futile.
I also tried insert this line below. I put it just before the last NEXT, but it didn't work as intended. I also attempted to insert some IF-Then statements to check for values in Year and Account before writing to the file and then clearing out the values after writing to the file. That almost worked, but my first line and last lines are not producing correct data.
objFileToWrite.WriteLine(strYear & "," & strAccount & "," & strCurrAmt & "," & strHasDelq & "," & strWarNum)
ok now that you've had a giggle with my prehistoric attempt at coding this, can you lend me a hand? :) let me know if anything else is needed. thanks for any time invested. I know some of you can likely kick this out with ease.