Re: xml parsing issue



"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 a
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"
IPAddress="10.128.129.128"/>
<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 />.

Thats
the first issue and second is based on sample xml in previous thread I
would
expect additonal vlan tags to get created.



--------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"
IPAddress="10.128.129.128"/>
<Network DeviceId="TestDevice" Number="1"
IPAddress="10.128.129.129"/>
</VLAN>
<VLAN Id="300">
<Network DeviceId="TestDevice" Number="1"
IPAddress="10.128.129.130"/>
</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


.



Relevant Pages

  • Re: Getting NASM from C
    ... > format we were going to edit it in... ... all XML is? ... HTML is a specific "subset" of XML for displaying ... it's a text file with "tags" inside ...
    (alt.lang.asm)
  • Re: Gotta start somewhere ... how many of us are really out there?
    ... if you are thinking of using XML ... pure shell commands ... ... or whatever XML tags you want to use using the acutal ... accompish all this in hindsight,as all the commands ...
    (freebsd-questions)
  • Re: Read binary data file
    ... I think its use is quite industry-dependent: I've never seen it used in financial messaging (that's more likely to use SWIFT formats, which are tagged text) but its common in the telecommunications industry. ... Compared with XML its a LOT more compact (tags are one byte, fixed length fields don't have terminators, variable length fields are preceded by a one or two byte length) and it has a number of predefined field types as well as arrays. ...
    (comp.lang.java.programmer)
  • Re: REZKONV und Apple OS X
    ... only MasterCook XML export format). ... (name of recipe) designates the Name element, ... These tags work with plain-text files or 3rd party XML files. ... There should be only one ingredient per line. ...
    (de.rec.mampf)
  • setfib+pf
    ... I'd like to ask on the best options for using setfib and pf in a non-BGP ... with VLANs for internal networks and want ... Vlan tagging also possible there. ... to make tags speak anything but network numbers. ...
    (freebsd-net)