![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | xml parsing issue I have an issue with the number of records coming back while parsing xml. Below is sample xml file used as a configuration file. I am parsing data and creating a new file with data needed. I have majority of parsing completed but ran into a snag. I did not put all the code in email and rather the snippets of the critical pieces. If I just use a For/next statement for getting VLAN nodes the output is 100 , 200, 300. Once I get the list and move to the next For statement to get values what happens is I end up getting the Network DeviceId for all vlans. My expectation is the following: 1. iterate through the first VLAN which happens to be 100 first. Create VLAN ID 100 in output file if its not created. 2. Read each network tag attribute for that VLAN and write out to file. Once I get the values for VLAN 100 and get the info needed for two network values, tag is closed 3. Then create vlan 200 and get its values and continue on until i am done. What is happening: 1. When I get the vlans the For/Next statement end up getting all the vlan Network attriubutes in the first loop. I added some debug info and when output the NetworknodeList with first loop it tell me the count is 5 which really should be 2. This tells me I am somehoe pulling in every network attribute for all VLANS and not just the current one and then moving on. Any ideas? Set NetworknodeList = xml.selectNodes("/MyNetwork/Networks/VLAN/Network") Set VLANNetworknodeList = xml.selectNodes("/MyNetwork/Networks/VLAN") For h = 0 To VLANNetworknodeList.length -1 sVLANId=VLANNetworknodeList(h).Attributes.getNamedItem("Id").value WScript.Echo " The VLAN ID is. " & sVLANId For i = 0 To NetworknodeList.length -1 WScript.Echo "Network Node List = " & NetworknodeList.length sDeviceId=NetworknodeList(i).Attributes.getNamedItem("DeviceId").value sBeginRange=NetworknodeList(i).Attributes.getNamedItem("BeginRange").value sEndRange=NetworknodeList(i).Attributes.getNamedItem("EndRange").value Next Next ----------XML FILE--------- <MyNetwork> <Networks> <VLAN Id="100"> <Network DeviceId="Router" BeginRange="1" EndRange="1"/> <Network DeviceId="Controller" BeginRange="1" EndRange="1"/> </VLAN> <VLAN Id="200"> <Network DeviceId="TestDevice" BeginRange="1" EndRange="1"/> <Network DeviceId="TestDevice" BeginRange="1" EndRange="1"/> </VLAN> <VLAN Id="300"> <Network DeviceId="Test Device" BeginRange="1" EndRange="1"/> </VLAN> </MyNetwork> |
My System Specs![]() |
| | #2 (permalink) |
| | RE: xml parsing issue "Big D" wrote: Quote: > > ----------XML FILE--------- > <MyNetwork> > <Networks> > <VLAN Id="100"> > <Network DeviceId="Router" BeginRange="1" EndRange="1"/> > <Network DeviceId="Controller" BeginRange="1" EndRange="1"/> > </VLAN> > <VLAN Id="200"> > <Network DeviceId="TestDevice" BeginRange="1" EndRange="1"/> > <Network DeviceId="TestDevice" BeginRange="1" EndRange="1"/> > </VLAN> > <VLAN Id="300"> > <Network DeviceId="Test Device" BeginRange="1" EndRange="1"/> > </VLAN> > </MyNetwork> You have a <Networks> tag but no matching </Networks>. I'm surprised it can be parsed, at all. |
My System Specs![]() |
| | #3 (permalink) |
| | RE: xml parsing issue Don't get all hung up on only using the selectNodes method. And don't forget about "containerization". When you do use selectNodes or getElementsByTagName or various other methods, you will *only* find nodes/attributes/whatever that are *INSIDE* the object you are using to make the method call! So lon as you persist in using the root xml object to do all your selectNodes, you *WILL* get *ALL* matching nodes. Period. So...a simple demo. This is done with VBScript in ASP, but just strip off the <%...%> and change Response.Write to WScript.echo and get rid of the HTML, and it should work perfectly. (Just try it as an ASP page first.) <% data = _ "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbNewLine & _ "<MyNetwork>" & vbNewLine & _ " <Networks>" & vbNewLine & _ " <VLAN Id=""100"">" & vbNewLine & _ " <Network DeviceId=""Router"" BeginRange=""1"" EndRange=""1""/>" & vbNewLine & _ " <Network DeviceId=""Controller"" BeginRange=""1"" EndRange=""1""/>" & vbNewLine & _ " </VLAN>" & vbNewLine & _ " <VLAN Id=""200"">" & vbNewLine & _ " <Network DeviceId=""TestDevice"" BeginRange=""1"" EndRange=""1""/>" & vbNewLine & _ " <Network DeviceId=""TestDevice"" BeginRange=""1"" EndRange=""1""/>" & vbNewLine & _ " </VLAN>" & vbNewLine & _ " <VLAN Id=""300"">" & vbNewLine & _ " <Network DeviceId=""Test Device"" BeginRange=""1"" EndRange=""1""/>" & vbNewLine & _ " </VLAN>" & vbNewLine & _ " </Networks>" & vbNewLine & _ "</MyNetwork>" Set dom = Server.CreateObject("msxml2.DomDocument") dom.LoadXML data Set vlans = dom.getElementsByTagName("VLAN") For Each vlan In vlans Response.Write "VLAN: id is " & vlan.Attributes.getNamedItem("Id").value & "<ul>" & vbNewLine Set vnets = vlan.getElementsByTagName("Network") For Each vnet In vnets Response.Write "<li>Network: " & vnet.Attributes.getNamedItem("DeviceId").value & "</li>" Next Response.Write "</ul>" & vbNewLine Next %> |
My System Specs![]() |
| | #4 (permalink) |
| | Re: xml parsing issue 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. 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. "Old Pedant" <OldPedant@xxxxxx> wrote in message news:6B62E364-6771-4538-B572-68504D696C86@xxxxxx Quote: > What the heck... > > Here, as a VBS file: > > data = _ > "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbNewLine & _ > "<MyNetwork>" & vbNewLine & _ > " <Networks>" & vbNewLine & _ > " <VLAN Id=""100"">" & vbNewLine & _ > " <Network DeviceId=""Router"" BeginRange=""1"" EndRange=""1""/>" & > vbNewLine & _ > " <Network DeviceId=""Controller"" BeginRange=""1"" EndRange=""1""/>" & > vbNewLine & _ > " </VLAN>" & vbNewLine & _ > " <VLAN Id=""200"">" & vbNewLine & _ > " <Network DeviceId=""TestDevice"" BeginRange=""1"" EndRange=""1""/>" & > vbNewLine & _ > " <Network DeviceId=""TestDevice"" BeginRange=""1"" EndRange=""1""/>" & > vbNewLine & _ > " </VLAN>" & vbNewLine & _ > " <VLAN Id=""300"">" & vbNewLine & _ > " <Network DeviceId=""Test Device"" BeginRange=""1"" EndRange=""1""/>" > & > vbNewLine & _ > " </VLAN>" & vbNewLine & _ > " </Networks>" & vbNewLine & _ > "</MyNetwork>" > > Set dom = CreateObject("msxml2.DomDocument") > > dom.LoadXML data > Set vlans = dom.getElementsByTagName("VLAN") > For Each vlan In vlans > WScript.Echo "VLAN: id is " & vlan.Attributes.getNamedItem("Id").value > Set vnets = vlan.getElementsByTagName("Network") > For Each vnet In vnets > WScript.Echo " Network: " & > vnet.Attributes.getNamedItem("DeviceId").value > WScript.Echo " BeginRange: " & > vnet.Attributes.getNamedItem("BeginRange").value > WScript.Echo " EndRange: " & > vnet.Attributes.getNamedItem("EndRange").value > Next > Next > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: xml parsing issue "Big D" <BigDaddy@xxxxxx> wrote in message news:OyxLTnI%23IHA.4772@xxxxxx Quote: > 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" Quote: > <Network DeviceId="TestDevice" Number="1" Quote: > <Network DeviceId="TestDevice" Number="1" Quote: > </Networks> > </Store> > > The VLAN tag and attribute get created but a closing tag never does. this: <VLAN></VLAN> can be shortened to <VLAN />. Quote: > Thats > the first issue and second is based on sample xml in previous thread I Quote: > 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" Quote: > <Network DeviceId="TestDevice" Number="1" Quote: > </VLAN> > <VLAN Id="300"> > <Network DeviceId="TestDevice" Number="1" Quote: > </VLAN> > </Networks> > </MyNetwork> > > Any help to get me pointed in the right direction is appreciated. > > 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 |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| XML parsing issue in IE for Windows Vista | Vista General | |||
| parsing xml | VB Script | |||
| Parsing Web content | PowerShell | |||
| PowerShell parsing issue | PowerShell | |||
| Getting a web page and parsing | PowerShell | |||