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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLDOM question?

Status
Not open for further replies.

Troy28M

Programmer
Jan 24, 2005
24
0
0
US
Need to know if it is possible to check whether an attribute exists? As you can see below, the same XML file will be used by multiple users. I want to check to see if the attribute "name" exists. If not -I will create. If so, I pull the value.

Before "Set nodSelect" statement, I would like to check to see if the name="tom" exists...possible?

XML:
<program>
<user name="steve">
<org>Lawn Movers</org>
<site>Los Angeles</site>
<pos>Gardner</pos>
<msi>20080204A</msi>
<updates>20080409</updates>
</user>
<user name="tom">
<org>Lawn Movers</org>
<site>Los Angeles</site>
<pos>Gardner</pos>
<msi>20080204A</msi>
<updates>20080409</updates>
</user>
</Program>

Parsing with:
Function readXML(byVal strValue, byValue sUser)
strXpath = "/program/user[@name='" & sUser & "']"
Set nodSelect = oXML.selectSingleNode(strXpath).childNodes
For Each x In nodSelect
If x.nodeName = strValue Then
readXML = x.text
End If
Next
End Function

 
[tt]Function readXML(byVal strValue, [red]byVal[/red] sUser)
[blue]dim strXpath,nodSelect,x[/blue]
strXpath = "/program/user[@name='" & sUser & "']"
[blue]Set nodSelect = oXML.selectSingleNode(strXpath)
if not(nodSelect is nothing) then[/blue]
For Each x In nodSelect[blue].childNodes[/blue]
If x.nodeName = strValue Then
readXML = x.text
[blue]exit for[/blue]
End If
Next
[blue]else
'create it
end if
set nodSelect=nothing[/blue]
End Function
[/tt]
 
Thanks tsuji, that works like a charm. New to VBScript and just learning the XML parser, so your help is much appreciated.

If you have any sites for learning XMLDOM, I could really use any and all help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top