Log in

View Full Version : Parsing multiple xml tags in file



wabonnett
10-11-2012, 12:55 PM
I’m having issues with parsing XML in an implementation of VBA that was customized to be used in a product called Vantive named Vantive Basic. After much searching on the net I’ve not been able to find the exact code that I need to parse the xml as I need.

Sample snippet of XML. There are many other elements outside of/below these, but these are the only ones that I’m concerned with.


<SHIPMENTDOCUMENTS>
<DOCUMENTS>
<DOCFORMAT>SED.STANDARD</DOCFORMAT>
<DOCUMENTURL>PAGE_1.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_2.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_3.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_4.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_n.html</DOCUMENTURL>
</DOCUMENTS>
<DOCUMENTS>
<DOCFORMAT>CI.STANDARD</DOCFORMAT>
<DOCUMENTURL>PAGE_1.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_2.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_3.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_4.html</DOCUMENTURL>
<DOCUMENTURL>PAGE_n.html</DOCUMENTURL>
</DOCUMENTS>
</SHIPMENTDOCUMENTS>


I can get to the documents tag, read the <DOCFORMAT> tag and only one <DOCUMENTURL> tag enclosed in each of the <DOCUMENTS> tags.

I need to read ALL of the <DOCUMENTURL>’s where <DOCFORMAT> = “SED.STANDARD” into one variable, and all of the <DOCUMENTURL>’s where <DOCFORMAT> = “CI.STANDARD”.

Here’s the code I currently have and works for a single <DOCUMENTURL> in each <DOCUMENTS>:


function test_parse
dim XDoc,oDocumentsList,oDocFormat,oDocumentURL as object
dim count as integer

set XDoc = CreateObject("Microsoft.XMLDOM")

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
a = objXMLDoc.load("C:\returned.xml")

Set oDocumentsList = objXMLDoc.documentElement.selectNodes("//SHIPMENTRESPONSE/RATEDSHIPMENT/SHIPMENTDOCUMENTS/DOCUMENTS")

' "//SHIPMENTRESPONSE/RATEDSHIPMENT/SHIPMENTDOCUMENTS/DOCUMENTS")

if oDocumentsList.length >= 1 then
For Each Node In oDocumentsList
msgbox node.selectSingleNode("DOCFORMAT").text

‘Need to loop through all the DOCUMENTURL TAGS HERE

msgbox node.selectSingleNode("DOCUMENTURL").text

'END Need to loop through all the DOCUMENTURL TAGS HERE
Next
else
msgbox "Nothing returned"
end if

set xdoc = nothing
set oDocumentsList = nothing
set oDocFormat = nothing
set oDocumentURL = nothing

end function



Any assistance that you can provide would be greatly appreciated. :beerchug:

Cheers, Wayne