vbscript and xml

Discussion in 'XML' started by Argus Rogue, Feb 1, 2008.

  1. Argus Rogue

    Argus Rogue Guest

    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>
     
    Argus Rogue, Feb 1, 2008
    #1
    1. Advertising

  2. Argus Rogue wrote:

    > 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


    > 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')]")

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 1, 2008
    #2
    1. Advertising

  3. Argus Rogue

    Argus Rogue Guest

    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" <> wrote in message
    news:47a3139c$0$9105$-online.net...
    > Argus Rogue wrote:
    >
    >> 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

    >
    >> 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')]")
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    Argus Rogue, Feb 1, 2008
    #3
  4. Argus Rogue wrote:
    > in your code, you read in only my file, how can i read an entire directory


    vbscript question, not XML question; XML doesn't know anything about
    directories.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 1, 2008
    #4
  5. Argus Rogue

    Argus Rogue Guest

    sorry, about that, newbie here...

    "Joseph Kesselman" <> wrote in message
    news:47a39db6$1@kcnews01...
    > Argus Rogue wrote:
    >> in your code, you read in only my file, how can i read an entire
    >> directory

    >
    > vbscript question, not XML question; XML doesn't know anything about
    > directories.
    >
    > --
    > Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Argus Rogue, Feb 2, 2008
    #5
  6. Argus Rogue wrote:

    > 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:


    I did point you to the FileSystemObject, you can use that to find files
    in a folder:
    <URL:http://msdn2.microsoft.com/en-us/library/ch28h2s7(VS.85).aspx>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 2, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc

    xml vbscript edit attribute

    bigbinc, Aug 19, 2003, in forum: XML
    Replies:
    0
    Views:
    720
    bigbinc
    Aug 19, 2003
  2. Marja Ribbers-de Vroed

    Classic VBscript and XML object serialization

    Marja Ribbers-de Vroed, Nov 29, 2005, in forum: ASP General
    Replies:
    1
    Views:
    670
    Bob Barrows [MVP]
    Nov 29, 2005
  3. Marja Ribbers-de Vroed

    [ASP/VBscript] Creating empty XML elements

    Marja Ribbers-de Vroed, Dec 6, 2005, in forum: ASP General
    Replies:
    4
    Views:
    162
    Gerry Hickman
    Dec 7, 2005
  4. mr_burns
    Replies:
    0
    Views:
    106
    mr_burns
    Dec 22, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    465
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page