Home All Groups Group Topic Archive Search About

Need a Script to pull specific data from an xml file



Author
28 Nov 2007 2:28 PM
Tfarmer
Ok what we are trying to do is make a script to search for and pull a job
number out of like 1000 xml files and put it into one file.
I'm a script newbie so any and all help would be greatly appreciated.

Thank you.

Author
28 Nov 2007 5:00 PM
Tom Lavedas
On Nov 28, 9:28 am, Tfarmer <Tfar***@discussions.microsoft.com> wrote:
> Ok what we are trying to do is make a script to search for and pull a job
> number out of like 1000 xml files and put it into one file.
> I'm a script newbie so any and all help would be greatly appreciated.
>
> Thank you.

Here is an example script that I picked up a long time ago ...

sFilePath = "testcase.xml"

sXPath = "/Versioning/Application"

Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLDoc.SetProperty "SelectionLanguage", "XPath"
oXMLDoc.Async = False

oXMLDoc.Load sFilePath

If (oXMLDoc.parseError.errorCode <> 0) Then

   Set oErr = oXMLDoc.ParseError
   WScript.Echo "Could not load file " & sFilePath _
              & " , error: " & oErr.Reason
   WScript.Quit
End If

Set dApps = CreateObject("Scripting.Dictionary")
dApps.CompareMode = vbTextCompare

Set oApps = oXMLDoc.DocumentElement.SelectNodes(sXPath)
For Each oApp in oApps
   dApps.Add oApp.getAttribute("Name"), oApp.getAttribute("Version")
Next

' enumerate the dictionary object, echoing both the key and item
value:
For Each sApp In dApps
    WScript.Echo "Applcation: " & sApp & " Version: " &
dApps.Item(sApp)
Next

' Checking if a spesific key exists, and display the item value
If dApps.Exists("State City") Then
    WScript.Echo "The application State City exists in the list,
version is " _
       & dApps.Item("State City")
End If

It parses an XML file structured like this ...

<?xml version="1.0" ?>
<Versioning>
<Application Name="IBDI Version" Version="2.7"
ENVName="IBDIADSVERSION"/>
<Application Name="State City" Version="3.9.0"
ENVName="STATECITYVERSION"/>
<Application Name="ICS" Version="9.11.0" ENVName="RCSVERSION"/>
<Application Name="Release" Version="3.5.009"
ENVName="RELEASEVERSION"/>
<Application Name="Web" Version="5.13.0" ENVName="WEBVERSION"/>
</Versioning>

The other part of your problem is searching a folder for XML files,
which is done something like this (based on MS Script Center
example) ...

strComputer = "."
sSomefolder = createobject("wscript.shell").currentdirectory ' for
example
sPathspec = Replace(Mid(sSomefolder,3), "\", "\\") & "\\"

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Drive = 'C:' " _
   & "AND Path = '" & sPathSpec & "' AND Extension = 'xml'")

with createobject("scripting.filesystemobject")_
  .opentextfile("jobs.txt",2,true)

  For Each objFile in colFiles
    .writeline FindJobinXML(objFile.Name)
  Next
end with

wsh.echo "Job search completed"

function FindJobinXML(Name)
'... you fill in the rest
  FindJobinXML = xmlresult
end function

TechNet Script Center Sample Scripts (URL all one line)
http://www.microsoft.com/downloads/details.aspx?FamilyID=b4cb2678-dafb-4e30-b2da-b8814fe2da5a

HTH

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Author
28 Nov 2007 5:16 PM
Tfarmer
Thanks a million :) I'll see if I can get this script working.

Show quote
"Tom Lavedas" wrote:

> On Nov 28, 9:28 am, Tfarmer <Tfar***@discussions.microsoft.com> wrote:
> > Ok what we are trying to do is make a script to search for and pull a job
> > number out of like 1000 xml files and put it into one file.
> > I'm a script newbie so any and all help would be greatly appreciated.
> >
> > Thank you.
>
> Here is an example script that I picked up a long time ago ...
>
> sFilePath = "testcase.xml"
>
> sXPath = "/Versioning/Application"
>
> Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
> oXMLDoc.SetProperty "SelectionLanguage", "XPath"
> oXMLDoc.Async = False
>
> oXMLDoc.Load sFilePath
>
> If (oXMLDoc.parseError.errorCode <> 0) Then
>
>    Set oErr = oXMLDoc.ParseError
>    WScript.Echo "Could not load file " & sFilePath _
>               & " , error: " & oErr.Reason
>    WScript.Quit
> End If
>
> Set dApps = CreateObject("Scripting.Dictionary")
> dApps.CompareMode = vbTextCompare
>
> Set oApps = oXMLDoc.DocumentElement.SelectNodes(sXPath)
> For Each oApp in oApps
>    dApps.Add oApp.getAttribute("Name"), oApp.getAttribute("Version")
> Next
>
> ' enumerate the dictionary object, echoing both the key and item
> value:
> For Each sApp In dApps
>     WScript.Echo "Applcation: " & sApp & " Version: " &
> dApps.Item(sApp)
> Next
>
> ' Checking if a spesific key exists, and display the item value
> If dApps.Exists("State City") Then
>     WScript.Echo "The application State City exists in the list,
> version is " _
>        & dApps.Item("State City")
> End If
>
> It parses an XML file structured like this ...
>
> <?xml version="1.0" ?>
> <Versioning>
>  <Application Name="IBDI Version" Version="2.7"
> ENVName="IBDIADSVERSION"/>
>  <Application Name="State City" Version="3.9.0"
> ENVName="STATECITYVERSION"/>
>  <Application Name="ICS" Version="9.11.0" ENVName="RCSVERSION"/>
>  <Application Name="Release" Version="3.5.009"
> ENVName="RELEASEVERSION"/>
>  <Application Name="Web" Version="5.13.0" ENVName="WEBVERSION"/>
> </Versioning>
>
> The other part of your problem is searching a folder for XML files,
> which is done something like this (based on MS Script Center
> example) ...
>
> strComputer = "."
> sSomefolder = createobject("wscript.shell").currentdirectory ' for
> example
> sPathspec = Replace(Mid(sSomefolder,3), "\", "\\") & "\\"
>
> Set objWMIService = GetObject("winmgmts:" _
>     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
> \cimv2")
> Set colFiles = objWMIService.ExecQuery _
>     ("Select * from CIM_Datafile Where Drive = 'C:' " _
>    & "AND Path = '" & sPathSpec & "' AND Extension = 'xml'")
>
> with createobject("scripting.filesystemobject")_
>   .opentextfile("jobs.txt",2,true)
>
>   For Each objFile in colFiles
>     .writeline FindJobinXML(objFile.Name)
>   Next
> end with
>
> wsh.echo "Job search completed"
>
> function FindJobinXML(Name)
>  '... you fill in the rest
>   FindJobinXML = xmlresult
> end function
>
> TechNet Script Center Sample Scripts (URL all one line)
> http://www.microsoft.com/downloads/details.aspx?FamilyID=b4cb2678-dafb-4e30-b2da-b8814fe2da5a
>
> HTH
>
> Tom Lavedas
> ===========
> http://members.cox.net/tglbatch/wsh/
>

AddThis Social Bookmark Button