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 - parsing xml

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


 
 

parsing xml

I am parsing an xml file but looking for advice on how to better approach.

How do I go about enumerating through the VLAN tags since they have a unqiue
number for each one. I am enumerating each Device attribute.

As of right now I am using a for loop that starts at 1 and goes up to 3. I
would like to just enumerate the tags instead of using a counter.

Code snippet - I left out the intialtialization of loading up xml.

Counter = 0
For h = 1 To 3
Set NetworknodeList = XML.selectNodes("//MyNetwork/Networks/VLAN" & h &
"00/Network")
For i = 0 To NetworknodeList.length -1
sDeviceId=NetworknodeList(i).Attributes.getNamedItem("DeviceId").value
sBeginRange=NetworknodeList(i).Attributes.getNamedItem("BeginRange").value
sEndRange=NetworknodeList(i).Attributes.getNamedItem("EndRange").value

For j = sBeginRange To sEndRange
WScript.Echo sDeviceId & " " & ip & Counter
Counter= Counter + 1
Next
Next
Next


----------SAMPLE XML FILE---------
<MyNetwork>

<Networks>

<VLAN100>

<Network DeviceId="Router" BeginRange="1" EndRange="1"/>

</VLAN100>

<VLAN200>

<Network DeviceId="Router" BeginRange="1" EndRange="1"/>

</VLAN200>

<VLAN300>

<Network DeviceId="Router" BeginRange="1" EndRange="1"/>

</VLAN300>

</MyNetwork>



My System SpecsSystem Spec
Old 08-04-2008   #2 (permalink)
Joe Fawcett


 
 

Re: parsing xml

That's why it's a poorly designed schema, having a VLAN element with an
attribute signifying the number would be better.
One approach would be to use: /MyNetwork/Networks/*/Network.
You don't need the expensive // operator at the start if MyNetwork is the
document element.
Alternatively: /MyNetwork/Networks/*[starts-with(local-name(),
'VLAN')]/Network

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name
"Big D" <BigDaddy@xxxxxx> wrote in message
news:Oexqspj9IHA.4092@xxxxxx
Quote:

> I am parsing an xml file but looking for advice on how to better approach.
>
> How do I go about enumerating through the VLAN tags since they have a
> unqiue number for each one. I am enumerating each Device attribute.
>
> As of right now I am using a for loop that starts at 1 and goes up to 3. I
> would like to just enumerate the tags instead of using a counter.
>
> Code snippet - I left out the intialtialization of loading up xml.
>
> Counter = 0
> For h = 1 To 3
> Set NetworknodeList = XML.selectNodes("//MyNetwork/Networks/VLAN" & h &
> "00/Network")
> For i = 0 To NetworknodeList.length -1
> sDeviceId=NetworknodeList(i).Attributes.getNamedItem("DeviceId").value
>
> sBeginRange=NetworknodeList(i).Attributes.getNamedItem("BeginRange").value
> sEndRange=NetworknodeList(i).Attributes.getNamedItem("EndRange").value
>
> For j = sBeginRange To sEndRange
> WScript.Echo sDeviceId & " " & ip & Counter
> Counter= Counter + 1
> Next
> Next
> Next
>
>
> ----------SAMPLE XML FILE---------
> <MyNetwork>
>
> <Networks>
>
> <VLAN100>
>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>
> </VLAN100>
>
> <VLAN200>
>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>
> </VLAN200>
>
> <VLAN300>
>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>
> </VLAN300>
>
> </MyNetwork>
>
>
My System SpecsSystem Spec
Old 08-04-2008   #3 (permalink)
Big D


 
 

Re: parsing xml

I implemented the following /MyNetwork/Networks/*/Network per your direction
and it worked.

For best practices should I remove the numbers following the VLAN and just
have VLAN tags. I need to idnetify each vlan with a number. Where should I
put the number? I updated example below but looking for best approach.

Thanks!

----------UPDATED SAMPLE XML FILE---------
<MyNetwork>
<Networks>
<VLAN>
<vlanID>1</vlanID>
<Network DeviceId="Router" BeginRange="1" EndRange="1"/>
</VLAN>
<VLAN>
<vlanID>2</vlanID>
<Network DeviceId="Router" BeginRange="1" EndRange="1"/>
</VLAN>
<VLAN>
<vlanID>3</vlanID>
<Network DeviceId="Router" BeginRange="1" EndRange="1"/>
</VLAN>
</MyNetwork>




"Joe Fawcett" <joefawcett@xxxxxx> wrote in message
news:upJJg2k9IHA.4536@xxxxxx
Quote:

> That's why it's a poorly designed schema, having a VLAN element with an
> attribute signifying the number would be better.
> One approach would be to use: /MyNetwork/Networks/*/Network.
> You don't need the expensive // operator at the start if MyNetwork is the
> document element.
> Alternatively: /MyNetwork/Networks/*[starts-with(local-name(),
> 'VLAN')]/Network
>
> --
>
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name
> "Big D" <BigDaddy@xxxxxx> wrote in message
> news:Oexqspj9IHA.4092@xxxxxx
Quote:

>> I am parsing an xml file but looking for advice on how to better
>> approach.
>>
>> How do I go about enumerating through the VLAN tags since they have a
>> unqiue number for each one. I am enumerating each Device attribute.
>>
>> As of right now I am using a for loop that starts at 1 and goes up to 3.
>> I would like to just enumerate the tags instead of using a counter.
>>
>> Code snippet - I left out the intialtialization of loading up xml.
>>
>> Counter = 0
>> For h = 1 To 3
>> Set NetworknodeList = XML.selectNodes("//MyNetwork/Networks/VLAN" & h &
>> "00/Network")
>> For i = 0 To NetworknodeList.length -1
>> sDeviceId=NetworknodeList(i).Attributes.getNamedItem("DeviceId").value
>>
>> sBeginRange=NetworknodeList(i).Attributes.getNamedItem("BeginRange").value
>> sEndRange=NetworknodeList(i).Attributes.getNamedItem("EndRange").value
>>
>> For j = sBeginRange To sEndRange
>> WScript.Echo sDeviceId & " " & ip & Counter
>> Counter= Counter + 1
>> Next
>> Next
>> Next
>>
>>
>> ----------SAMPLE XML FILE---------
>> <MyNetwork>
>>
>> <Networks>
>>
>> <VLAN100>
>>
>> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>>
>> </VLAN100>
>>
>> <VLAN200>
>>
>> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>>
>> </VLAN200>
>>
>> <VLAN300>
>>
>> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
>>
>> </VLAN300>
>>
>> </MyNetwork>
>>
>>

My System SpecsSystem Spec
Old 08-04-2008   #4 (permalink)
Old Pedant


 
 

Re: parsing xml



"Big D" wrote:
Quote:

> For best practices should I remove the numbers following the VLAN and just
> have VLAN tags. I need to idnetify each vlan with a number. Where should I
> put the number? I updated example below but looking for best approach.
Quote:

> ----------UPDATED SAMPLE XML FILE---------
> <MyNetwork>
> <Networks>
> <VLAN>
> <vlanID>1</vlanID>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
> </VLAN>
> <VLAN>
> <vlanID>2</vlanID>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
> </VLAN>
> <VLAN>
> <vlanID>3</vlanID>
> <Network DeviceId="Router" BeginRange="1" EndRange="1"/>
> </VLAN>
> </MyNetwork>
That works, but if you ever want to be able to select a single VLAN based on
its id, you'd have to find the vlanID node first and then go back up a level
to get to the VLAN, per se.

So why not simply us
<VLAN id="1">....</VLAN>
<VLAN id="2">....</VLAN>
etc.

???

Now you can do an XPath query to get any particular VLAN quite easily.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Data parsing - .NET PowerShell
xml parsing issue VB Script
Parsing Web content PowerShell
Command parsing PowerShell


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