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!

how to solve this problem in XML!!!

Status
Not open for further replies.

rockfido

Technical User
Apr 27, 2010
6
US
Hi, everyone, I need to read a tons of xml files like this one:

<?xml version="1.0" encoding="UTF-8"?>
<IM>
<CR ID1="13" ID2="460">
</CR>
</IM>

my code in VBA is like:

For i = 1 To 100 'suppose i have 100 xml files

Filename = Sheet1.Cells(i, 1) 'where i store the file path
Set Doc = New DOMDocument

Set IM = Doc.DocumentElement

For Each CR In IM.ChildNodes

Set Attribute_CR = CR.Attributes

Set ID1 = Attribute_CR.getNamedItem("ID1")
Debug.Print ID1.Text

.....

Well, it works well. But my question is that, actually during some xml file, it doesnt always have ID1, (sometimes, they only have ID2 etc). So this code will not work for those files do not have ID1. But what should I to code it in general?

Thanks a lot

 
>Debug.Print ID1.Text
[tt]If not ID1 is nothing then
Debug.Print ID1.Text
else
Debug.Print "Attribute named """ & "ID1" & """ does not exist."
end if
[/tt]
 
Thank you so much. That's exactly what i need!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top