XML Parse help

Started by Trippinout, September 29, 2014, 06:45:35 AM

Previous topic - Next topic

Trippinout

Not sure where I am going wrong, I don't have much experience with XML.
I took what I thought I would need from the help files.
I am trying to parse each Script section in Scripts but I get a 3131 error when trying to read the first element scriptname.

my xml.
<?xml version="1.0" encoding="UTF-8"?>
-<Config>-<Property platform="Windows 2000,x86" type="Generic" name="VMWare Workstation 10.0.1" description="VMWare Workstation 10.0.1\n20131126 1530" category="LOBGeneral" author="Kyle Adamson">
<!--Important information that needs to be kept out of Marimba but may not be needed in SCCM.-->
</Property>-<Reboot showdialog="true" allowcancel="true" allow="true" force="true">
<!--Determine if the application needs to reboot and whether to display a message or allow the user to cancel.-->
</Reboot>-<MSI>
<!--MSI attributes include transforms, patches, product and upgrade codes, command line options, and if the msiexec command line needs to be overriden.-->
</MSI>-<Dependencies>
<!--Dependencies include application, disk size, file and registry checks.-->
</Dependencies>-<Scripts>
<!--Runs is the sum of the following values, install=1, uninstall=2, update=4, repair=8, pre-script=16, post-script=0 & install=-1-->
<Script name="Prerequisites Check" ignorerc="False" runs="1" command=""PreReq.exe" VMWareWorkstation10.0.1_PR.cfg"/><Script name="Install VMWare Workstation" ignorerc="False" runs="1" command=""Install.bat" "/><Script name="Remove Source Directory" ignorerc="False" runs="1" command=""del_VMWKST.bat" "/><Script name="Uninstall VMWare Workstation" ignorerc="False" runs="2" command=""Uninstall.bat" "/><Script name="VMware Workstation 10.x Uninstall Cleanup" ignorerc="False" runs="2" command=""VMW10xUC.exe" "/></Scripts>-<EnvVariables>
<!--Environment variables that need to be added to the system.-->
</EnvVariables>-<ReturnCodes>
<!--These are non-default sucess/failure return codes for the application as a whole.-->
</ReturnCodes>-<ScriptReturnCodes>
<!--These are non-default sucess/failure return codes for all scripts.-->
</ScriptReturnCodes><Regset Value=""VMWARE WORKSTATION" "10.0.1"" set="true"/></Config>



Code
xfile = "settings.xml"



;xfile = DirScript() : "menu.xml"
xmlDoc = ObjectCreate("Msxml2.DOMDocument.4.0")
xmlDoc.async = @FALSE
xmlDoc.load(xfile)
err = xmlDoc.parseerror
If err.errorCode Then GoSub ShowParseErrors
;Returns a list of all descendant elements that match the supplied name.
;debug(@on)
objElements = xmlDoc.getElementsByTagName("Scripts") ;Tag name is CASE sensitive!
ForEach Element In objElements
   scriptname    = Element.ChildNodes(0).ChildNodes(0).text
   ignorerc = Element.ChildNodes(1).ChildNodes(0).text
   runs    = Element.ChildNodes(2).ChildNodes(0).text
   command    = Element.ChildNodes(3).ChildNodes(0).text

   Message(scriptname, "ignorerc=":ignorerc:@CRLF:"runs=":runs:@CRLF:"command=":command)

Next
objElements = 0
xmlDoc = 0
Exit

JTaylor

Part of your problem, and maybe all, is in the use of ChildNodes.  There are no nodes within the the Script nodes.  I think you want "GetAttribute" instead.  If this is the entire document I would suggest interrogating "Script" instead of "Scripts" for the GetElementsByTagname().   That will eliminate a level you have to navigate.

Jim

stanl

Adding to what Jim wrote. Consider using

Msxml2.DOMDocument.6.0

instead of

Msxml2.DOMDocument.4.0   <---- very buggy

Trippinout

Can you give me a little bit more on how to get each Script from Scripts?

objNode = XmlDoc.selectSingleNode('/Config/Scripts/Script')
   If objNode != 0
      Pause("Script name", objNode.getAttribute('name'))
      Pause("ignorerc", objNode.getAttribute('ignorerc'))
      Pause("runs", objNode.getAttribute('runs'))
      Pause("command", objNode.getAttribute('command'))
   EndIf

This works for the first one. I am just not sure how to itemize the "Script"

JTaylor

Untested but something like the following...


Code (winbatch) Select

objElements = xmlDoc.getElementsByTagName("Script") ;Tag name is CASE sensitive!
ForEach objNode In objElements

  Pause("Script name", objNode.getAttribute('name'))
  Pause("ignorerc", objNode.getAttribute('ignorerc'))
  Pause("runs", objNode.getAttribute('runs'))
  Pause("command", objNode.getAttribute('command'))

Next


DAG_P6

Since a typical XML document is organized somewhat like the folders of a file system, I find it expedient to open the document in either a Web browser or a text editor. If the document is neatly indented, both work equally well, and the editor will probably load it more quickly, since it doesn't parse it, but if the document is formatted compactly, without indenting, or, worse, without line breaks (think configuration files generated by Visual Studio), the Web browser view is much better for visualizing its structure. Even when the XML document is of my own design, I find it much easier to work out how to parse it if I have a copy of the document open while I write the parser.
David A. Gray
You are more important than any technology.