Re: xml parsing issue
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Fri, 8 Aug 2008 10:00:50 +0100
"Big D" <BigDaddy@xxxxxxxxxxxxxxxx> wrote in message
news:OyxLTnI%23IHA.4772@xxxxxxxxxxxxxxxxxxxxxxx
Thanks for help. I have made changes and gotten much further. I am at aIPAddress="10.128.129.128"/>
point all data is being created and writing to xml file what I need. I
started creating the xml tags as I get the data from config file and could
use some further direction.
The output file has the following tags:
<MyNetwork>
</MyNetwork>
As I get the data I create additonal tags an add data. I could be over
complicating when creating the tags.
----------------Code------------------
Set xmlMyNetworkNode = xml.documentElement.selectSingleNode("/MyNetwork")
Set xmlNetworksNode =
xml.documentElement.selectSingleNode("/MyNetwork/Networks")
Set xmlVLANNetworksNode =
xml.documentElement.selectSingleNode("/MyNetwork/Networks/VLAN")
Set xmlNetworkNode =
xml.documentElement.selectSingleNode("/MyNetwork/Networks/VLAN/Network")
If(xmlNetworksNode Is nothing) Then
Set xmlNetworksNode=xml.createNode(1, "Networks", "")
xmlMyNetworkNode.appendChild(xmlNetworksNode)
End If
If(xmlVLANNetworksNode Is nothing) Then
Set xmlVLANNetworksNode=xml.createNode(1, "VLAN", "")
xmlNetworksNode.appendChild(xmlVLANNetworksNode)
xmlVLANNetworksNode.setAttribute "Id", sVLANId
xmlNetworksNode.appendChild xmlVLANNetworksNode
End If
if(xmlNetworkNode is nothing) Then
Set xmlNetworkNode = xml.createNode(1, "Network", "")
xmlMyNetworkNode.appendChild xmlNetworkNode
xmlNetworkNode.setAttribute "DeviceId", sDeviceId
xmlNetworkNode.setAttribute "Number", NumCounter
xmlNetworkNode.setAttribute "IPAddress", intIP
xmlNetworksNode.appendChild xmlNetworkNode
xml.save XML_DROPOFF_DIR & XML_FileName
End If
---------RESULT----------
<MyNetwork>
<Networks>
<VLAN Id="100"/>
<Network DeviceId="Router" Number="1" IPAddress="10.128.129.1"/>
<Network DeviceId="Controller" Number="1" IPAddress="10.128.129.4"/>
<Network DeviceId="TestDevice" Number="1"
<Network DeviceId="TestDevice" Number="1"IPAddress="10.128.129.129"/>
<Network DeviceId="TestDevice" Number="1"IPAddress="10.128.129.130"/>
</Networks>
</Store>
The VLAN tag and attribute get created but a closing tag never does.
Note the VLAN tag is terminate by the /> in XML a node that would look like
this: <VLAN></VLAN> can be shortened to <VLAN />.
Thatswould
the first issue and second is based on sample xml in previous thread I
expect additonal vlan tags to get created.IPAddress="10.128.129.128"/>
--------EXPECTED RESULT--------
<MyNetwork>
<Networks>
<VLAN Id="100">
<Network DeviceId="Router" Number="1" IPAddress="10.128.129.1"/>
<Network DeviceId="Controller" Number="1" IPAddress="10.128.129.4"/>
</VLAN>
<VLAN Id="200">
<Network DeviceId="TestDevice" Number="1"
<Network DeviceId="TestDevice" Number="1"IPAddress="10.128.129.129"/>
</VLAN>IPAddress="10.128.129.130"/>
<VLAN Id="300">
<Network DeviceId="TestDevice" Number="1"
</VLAN>
</Networks>
</MyNetwork>
Any help to get me pointed in the right direction is appreciated.
Is the above in a function which is called multiple times? One basic error
is you are appending the Network node to the Networks node where you should
be appending it to the VLAN node.
I guess you are calling the above code multiple times, how were you
expecting to create multiple VLAN nodes when your code selects the first
VLAN and if found doesn't create another.
Without further details about the context in which you are calling this code
its difficult to help. However here is a complete VBScript which generates
the expect XML. You can probably use the functions therein to make you code
clearer.
BTW, Functions, Functions, admins that do scripting really need to learn
the art of abstraction by functions. It would make what you are doing so
much clearer to you.
Option Explicit
'Generally useful XML functions
'(Only one needed for this example)
Function AddElem(elemParent, name)
Set AddElem = elemParent.ownerDocument.createElement(name)
elemParent.appendChild AddElem
End Function
'Specifically useful functions that hide much of the XML plumbing.
Function CreateMyNetwork()
Dim dom : Set dom = CreateObject("MSXML2.DOMDocument.3.0")
dom.loadXML("<MyNetwork />")
Set CreateMyNetwork = dom.documentElement
End Function
Function AddVLan(elemParent, id)
Set AddVLan = AddElem(elemParent, "VLAN")
AddVLan.setAttribute "id", id
End Function
Function AddNetwork(elemParent, deviceId, number, ipAddress)
Set AddNetwork = AddElem(elemParent, "Network")
AddNetwork.setAttribute "DeviceId", deviceId
AddNetwork.setAttribute "Number", number
AddNetwork.setAttribute "ipAddress", ipAddress
End Function
'The actual code to create the XML.
'Note how the expected structure is more apparent in
'the code itself.
Dim elemMyNetwork : Set elemMyNetWork = CreateMyNetwork()
Dim elemNetworks : Set elemNetworks = AddElem(elemMyNetwork, "Networks")
Dim elemVLan
Set elemVLan = AddVLan(elemNetworks, 100)
AddNetwork elemVLan, "Router", 1 , "10.128.129.1"
AddNetwork elemVLan, "Controller", 1 , "10.128.129.4"
Set elemVLan = AddVLan(elemNetworks, 200)
AddNetwork elemVLan, "TestDevice", 1 , "10.128.129.128"
AddNetwork elemVLan, "TestDevice", 1 , "10.128.129.129"
Set elemVLan = AddVLan(elemNetworks, 300)
AddNetwork elemVLan, "TestDevice", 1 , "10.128.129.130"
elemMyNetwork.ownerDocument.save "g:\temp\test.xml"
In one real world usage the creation of each VLan would be part of a for
loop and the creation of each Network would be an inner loop.
--
Anthony Jones - MVP ASP/ASP.NET
.
- References:
- xml parsing issue
- From: Big D
- xml parsing issue
- Prev by Date: Camera Automation under Vista
- Next by Date: Passing of variables
- Previous by thread: RE: xml parsing issue
- Next by thread: StdOut and StdErr reading...
- Index(es):
Relevant Pages
|