Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - testing an xml attribute that does not exist

Reply
 
Old 08-14-2008   #1 (permalink)
Big D


 
 

testing an xml attribute that does not exist

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


My System SpecsSystem Spec
Old 08-14-2008   #2 (permalink)
ekkehard.horner


 
 

Re: testing an xml attribute that does not exist

Big D schrieb:
Quote:

> 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) ========

My System SpecsSystem Spec
Old 08-14-2008   #3 (permalink)
Anthony Jones


 
 

Re: testing an xml attribute that does not exist

"Big D" <BigDaddy@xxxxxx> wrote in message
news:uk68DHg$IHA.1224@xxxxxx
Quote:

> 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
Quote:

> 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
Quote:

> 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
Quote:

> exists before doing anything. I am assuming if I can test the value that
an
Quote:

> 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").valu
e 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
Quote:

>
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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Using the FlagsAttribute Attribute PowerShell
User attribute PowerShell
explorer file attribute Vista General
How to insert the "modified time" attribute in "date taken" attribute in batch mode-in vista or theough a software? Vista file management
What is file attribute N? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46