![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 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 Quote: > exists before doing anything. I am assuming if I can test the value that 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 > 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: > 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 Specs![]() |
![]() |
| 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 | |||