Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

testing an xml attribute that does not exist

1,198 views
Skip to first unread message

Big D

unread,
Aug 14, 2008, 7:34:49 AM8/14/08
to
My issue is I am selecting data from an xml file in which I do not own and
some of the element attributes either exist or dont which causes issues from
script. See below.

------Sample XML--------

<Network>
<Kiosks>
<Kiosk Number="1" Name="Kiosk01" IPAddress="10.160.25.6" Model="Dell"
MacAddress="2211223344"/>
<Kiosk Number="2" Name="Kiosk02" IPAddress="10.160.25.7" />
<Kiosk Number="3" Name="Kiosk03" IPAddress="10.160.25.8" />
<Kiosk Number="4" Name="Kiosk04" IPAddress="10.160.25.9" Model="Dell"
MacAddress="0011223344" />
</Kiosks>
</Network>

Since I am interating through kiosk elements and in two of the 4 cases the
Model and MacAddress exist the script will not blow up. Once I hit a element
where it doesn't exist the script get an error.

Since I am grabbing the xml value how do I go about testing if the attribute
exists before doing anything. I am assuming if I can test the value that an
IF statement needs added to test wether the value is empty or NULL thyen
proceed. I just don't know how to test if the value is present.

------------Code snippet from script---------------------
Set KiosknodeList = xml.selectNodes("//Network/Kiosks/Kiosk")

For k = 0 To KiosknodeList.length -1

KioskNum=KiosknodeList(k).Attributes.getNamedItem("Number").value
MACAddress=KiosknodeList(k).Attributes.getNamedItem("MacAddress").value
intKioskIPAddress=KiosknodeList(k).Attributes.getNamedItem("IPAddress").value KioskModel=KiosknodeList(k).Attributes.getNamedItem("Model").value If KioskModel = "Dell" Then WScript.Echo "Found " & KioskModel Else Wscript.Echo "It's not a Dell model" End IfNext

Anthony Jones

unread,
Aug 14, 2008, 9:04:19 AM8/14/08
to
"Big D" <BigD...@newsgroup.nospam> wrote in message
news:uk68DHg$IHA....@TK2MSFTNGP02.phx.gbl...

For Each node in xml.selectNodes("//Network/Kiosks/Kiosk")

KioskNum = node.getAttribute("Number")
MACAddress = node.getAttribute("MacAddress")
KioskIPAddress = node.getAttribute("IPAddress")
KioskModel = node.getAttribute("Model")

If IsNull(KioskModel) Then
WScript.Echo "No Model provided"
Else
WScript.Echo "Model: " & KioskModel
End If

Next


--
Anthony Jones - MVP ASP/ASP.NET


ekkehard.horner

unread,
Aug 14, 2008, 8:54:06 AM8/14/08
to
Big D schrieb:

> My issue is I am selecting data from an xml file in which I do not own and
> some of the element attributes either exist or dont which causes issues from
> script. See below.
>
>
>
> ------Sample XML--------
>
> <Network>
> <Kiosks>
> <Kiosk Number="1" Name="Kiosk01" IPAddress="10.160.25.6" Model="Dell"
> MacAddress="2211223344"/>
> <Kiosk Number="2" Name="Kiosk02" IPAddress="10.160.25.7" />
> <Kiosk Number="3" Name="Kiosk03" IPAddress="10.160.25.8" />
> <Kiosk Number="4" Name="Kiosk04" IPAddress="10.160.25.9" Model="Dell"
> MacAddress="0011223344" />
> </Kiosks>
> </Network>
[...]
Use .getAttribute() and test for IsNull() as in this demo script:

Dim dicAtts : Set dicAtts = CreateObject( "Scripting.Dictionary" )
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( ".\kiosk.xml" )

dicAtts( "Number" ) = ""
dicAtts( "Name" ) = ""
dicAtts( "IPAddress" ) = ""
dicAtts( "Model" ) = ""
dicAtts( "MacAddress" ) = ""

oXDoc.async = False
oXDoc.load sFSpec
If 0 = oXDoc.ParseError Then
Dim ndlKiosk : Set ndlKiosk = oXDoc.selectNodes( "//Network/Kiosks/Kiosk" )
Dim ndKiosk
For Each ndKiosk In ndlKiosk
Dim sAttr
For Each sAttr In dicAtts.Keys
Dim vAttr : vAttr = ndKiosk.getAttribute( sAttr )
If IsNull( vAttr ) Then
dicAtts( sAttr ) = "no such attribute"
Else
dicAtts( sAttr ) = vAttr
End If
Next
For Each sAttr In dicAtts.Keys
WScript.Echo sAttr, dicAtts( sAttr )
Next
WScript.Echo
Next
Else
WScript.Echo oXDoc.ParseError.Reason
End If

output:

=== KioskXml: attr prob in kiosk.xml ===
Number 1
Name Kiosk01
IPAddress 10.160.25.6
Model Dell
MacAddress 2211223344

Number 2
Name Kiosk02
IPAddress 10.160.25.7
Model no such attribute
MacAddress no such attribute

Number 3
Name Kiosk03
IPAddress 10.160.25.8
Model no such attribute
MacAddress no such attribute

Number 4
Name Kiosk04
IPAddress 10.160.25.9
Model Dell
MacAddress 0011223344

=== KioskXml: 0 done (00:00:00) ========

0 new messages