vbscript and xml

Argus Rogue

Usenet Archive
Joined
Feb 1, 2008
Messages
3
Reaction score
0
Hello All,

this is another post (also posted in alt.comp.lang.vbscript) and I was wondering if it was possible to write a vbscript to that will do the following:

1. read each xml file from a directory
2. search each xml file (status) for the text fail
3. write to output file in HTML format the status and the file name to where you have the following:
Status Name
Fail name of the file

Set ObjFSO = CreateObject("Scripting.FileSystemObject") ObjStartFolder = "D:\Result_files\XML" Set Objfolder = ObjFSo.GetFolder(objstartfolder) Set ColFiles = Objfolder.files

For Each Objfile in ColFiles
strFileName = objfile.name
wscript.echo strFileName & VBCRLF
Dim objXML, Root, x
set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "false"
objXML.load(strFileName)
Set Root = objXML.documentElement
For Each x In Root.childNodes
WScript.Echo x.text & VBCRLF <--would like to write to a file instead Next Next

I found some code to read each file in the directory (part 1) but how do i do 2 and 3. Any and all help in this matter is greatly appreciated.

Also Listed below is a sample of my result file.xml

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="http://myweb.test.org/enterprisetesting/testharness/formatting/xslformat_verbose.xsl" ?><testresult><entry> <level>0</level> <status>info</status><name>Configuration File Path</name><version>3.9</version> <stepCounter>844</stepCounter> <Description>C:\AutomationConfiguration\configuration.ini</Description> <timestamp>12:40:09 PM</timestamp> <duration>0</duration> <link></link> </entry> <entry> <level>4</level> <status>DataTable Path</status><name>C:\Automation\Automation_Test_1.xls</name><stepCounter>844</stepCounter> <Description></Description> <timestamp>12:40:09 PM</timestamp> <duration>0</duration> <link></link> </entry> <entry> <level>0</level> <status>enter-scenario</status><name>C:\Automation\Automation_Test_2.xml</name><stepCounter>844</stepCounter> <Description></Description> <timestamp>12:40:10 PM</timestamp> <duration>0</duration> <link></link> </entry> <entry> <level>0</level> <status>fail</status><name>Automation_Test_2</name><stepCounter>846</stepCounter> <Description>C:\Automation\Automation_Test_2.xml</Description> <timestamp>12:40:10 PM</timestamp> <duration></duration> <link></link> </entry>
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Argus said:
1. read each xml file from a directory
2. search each xml file (status) for the text fail
3. write to output file in HTML format the status and the file name to where you have the following:
Status Name
I found some code to read each file in the directory (part 1) but how do i do 2 and 3.

3. is off topic here in comp.text.xml but generally with VBScript or JScript you have the Scripting.FileSystemObject to read and write text files (and a HTML file is a text file). So check <URL:http://msdn2.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx>

As for 2., you can use MSXML with script to parse XML documents into a document object model, then using MSXML 3 and later you have XPath support to find nodes. It is currently not clear to me whether you want to find an element named 'fail' or the text 'fail' in some element or attribute content. Your example XML document has an element named 'status' with the contents 'fail' so assuming you want to look for such elements you could do e.g. Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0") xmlDoc.async = False If xmlDoc.load("file.xml") Then xmlDoc.setProperty "SelectionLanguage", "XPath" Set status = xmlDoc.selectSingleNode("//status[. = 'fail']") If Not status Is Nothing Then ' element was found, write that to file Else ' element was not found End If Else ' XML document could not be parsed ' examine xmlDoc.parseError here End If

If you want to look for any element with the contents 'fail' then use xmlDoc.selectSingleNode("//*[. = 'fail']") if you want to look for elements containing the string 'fail' then use xmlDoc.selectSingleNode("//*[contains(., 'fail')]")
 

Argus Rogue

Usenet Archive
Joined
Feb 1, 2008
Messages
3
Reaction score
0
Hey Martin,

Thanks for the reply, it is greatly appreciated. Just one question though, in your code, you read in only my file, how can i read an entire directory and then produce the results of:

Fail file.xml
Fail file2.xml
Fail file12.xml
and etc...

Martin Honnen said:
Argus said:
1. read each xml file from a directory
2. search each xml file (status) for the text fail
3. write to output file in HTML format the status and the file name to where you have the following:
Status Name
I found some code to read each file in the directory (part 1) but how do i do 2 and 3.

3. is off topic here in comp.text.xml but generally with VBScript or JScript you have the Scripting.FileSystemObject to read and write text files (and a HTML file is a text file). So check <URL:http://msdn2.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx>

As for 2., you can use MSXML with script to parse XML documents into a document object model, then using MSXML 3 and later you have XPath support to find nodes. It is currently not clear to me whether you want to find an element named 'fail' or the text 'fail' in some element or attribute content. Your example XML document has an element named 'status' with the contents 'fail' so assuming you want to look for such elements you could do e.g. Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0") xmlDoc.async = False If xmlDoc.load("file.xml") Then xmlDoc.setProperty "SelectionLanguage", "XPath" Set status = xmlDoc.selectSingleNode("//status[. = 'fail']") If Not status Is Nothing Then ' element was found, write that to file Else ' element was not found End If Else ' XML document could not be parsed ' examine xmlDoc.parseError here End If

If you want to look for any element with the contents 'fail' then use xmlDoc.selectSingleNode("//*[. = 'fail']") if you want to look for elements containing the string 'fail' then use xmlDoc.selectSingleNode("//*[contains(., 'fail')]")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top