Jump to content

Help reading XML file


 Share

Recommended Posts

Need help!

I use the XML DOM wrapper.

xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<Deployment>
  <Properties>
    <Property name="INSTALLDIR">THIS VALUE</Property>
    <Property name="installLanguage">xxx</Property>
    <Property name="serialNumber">xxx</Property>
  </Properties>
</Deployment>

I want to get THIS VALUE.

Tried a lot but always get error or "-1"

Link to comment
Share on other sites

Something like this?

#include<_XMLDomWrapper.au3>
#include<Array.au3>
_XMLFileOpen(@ScriptDir &'\XMLTestfile.xml')
$re = _XMLGetValue("/Deployment/Properties/Property")
_ArrayDisplay($re)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Something like this?

#include<_XMLDomWrapper.au3>
#include<Array.au3>
_XMLFileOpen(@ScriptDir &'\XMLTestfile.xml')
$re = _XMLGetValue("/Deployment/Properties/Property")
_ArrayDisplay($re)

thx this works for me! :)

But how can i modify this value now? is _XMLSetAttrib the right function?

and is there somewhere a detailed documentation for XMLDomWrapper?

Edited by Surio
Link to comment
Share on other sites

#include "_XMLDomWrapper.au3"


_XMLFileOpen("XMLTestFile.xml")

;Get the value
$numberOfNodes = _XMLGetNodeCount("/Deployment/Properties/Property")
For $i = 1 to $numberOfNodes
    If _XMLGetAttrib("/Deployment/Properties/Property[" & $i & "]","name") = "INSTALLDIR" then Msgbox(0,"", _XMLGetFirstValue("/Deployment/Properties/Property[" & $i & "]"))
Next

;Set the new value
For $i = 1 to $numberOfNodes
    If _XMLGetAttrib("/Deployment/Properties/Property[" & $i & "]","name") = "INSTALLDIR" then _XMLUpdateField("/Deployment/Properties/Property[" & $i & "]","This is the new value")
Next

;Get the new Value
For $i = 1 to $numberOfNodes
    If _XMLGetAttrib("/Deployment/Properties/Property[" & $i & "]","name") = "INSTALLDIR" then Msgbox(0,"", _XMLGetFirstValue("/Deployment/Properties/Property[" & $i & "]"))
Next

Func _XMLGetFirstValue($node)

    Local $ret_val

    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf

EndFunc

Link to comment
Share on other sites

  • 2 weeks later...

Can this UDF be used for reading a WinAudit-generated XML. If so, how? (Im all new with both XML and AutoIT so bear with me)

Example of the XML below:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit</title>
    <category title="System Overview">
        <subcategory title="">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Computer Name</fieldvalue>
                    <fieldvalue>MyComputer1234</fieldvalue>
                </datarow>
                ...

However the problem remains:

how can I make the script find for example fieldvalue "Computer Name" and list all fieldvalues inside that <datarow> as search-results?

Edited by neebo
Link to comment
Share on other sites

That UDF can be used with any valid XML. Download the UDF and the examples from the topic in Example Scripts and try it out. If you get stuck, post what you tried.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Ok, heres the """code""" so far + example xml-file.

It reads all the XML files on the folder, searching "Item" + "Value" given.

However, some problems:

- after reading the last xml-file on the folder it stays on the loop forever.

- furthermore, it fails to read any data on xml-file beyond first CATEGORY-tag.

Any help greatly appreciated.

#include"_XMLDomWrapper.au3"

$reportFile = @DesktopDir & "\CSV.txt"
$Folder = "c:\_diileri"

FileDelete($reportFile)
FileOpen($reportFile, 9)


; #########################################################

; search files

$search = FileFindFirstFile(@ScriptDir & '\*.xml')
MsgBox(0, "SEARCH INIT:", $search, 2)

If $search = -1 Then ; ERROR
    MsgBox(0, "ERROR", "FILE NOT FOUND", 3)
    Exit
EndIf

; #########################################################

$searchItem = "Computer name"
$searchValue = "Rav"

; ######################################################

While 1 ; search all xml files on the folder

    $file = FileFindNextFile($search)
    MsgBox(0, $file, $search, 2)

    If @error then ExitLoop(2)

    _XMLFileOpen($file) ; xmlFile to be opened

    $iCount = _XMLGetNodeCount("/computeraudit/category/subcategory/recordset/datarow/fieldvalue")

    For $i = 1 to $iCount
        $XMLitem = _XMLGetFirstValue("/computeraudit/category/subcategory/recordset/datarow[" & $i & "]/fieldvalue")
        $XMLvalue = _XMLGetSecondValue("/computeraudit/category/subcategory/recordset/datarow[" & $i & "]/fieldvalue")
        ;$XMLdata  = _XMLGetThirdValue("/computeraudit/category/subcategory/recordset/datarow[" & $i & "]/fieldvalue")

        $subSearchItem = StringInStr($XMLitem, $searchItem, 2)
        $subSearchValue = StringInStr($XMLvalue, $searchValue, 2)

        If $subSearchItem And $subSearchValue Then
            MsgBox(0, "HIT", $XMLitem & @CRLF & $XMLvalue, 2)
            FileWrite($reportFile, $file & "     " & "ITEM: " & $XMLitem & "     " & "VALUE: " & $XMLvalue & @CRLF) ; write data
            ExitLoop(1)

        EndIf

    Next

    FileClose($file) ; added this

WEnd

FileClose($file)
FileClose($reportFile)

Exit

; #### Functions

Func _XMLGetFirstValue($node)

    Local $ret_val1

    $ret_val1 = _XMLGetValue($node)
    If IsArray($ret_val1) Then
        Return ($ret_val1[1])
    Else
        Return SetError(1,3,0)
    EndIf

EndFunc

Func _XMLGetSecondValue($node)

    Local $ret_val2

    $ret_val2 = _XMLGetValue($node)
    If IsArray($ret_val2) Then
        Return ($ret_val2[2])
    Else
        Return SetError(1,3,0)
    EndIf

EndFunc

Func _XMLGetThirdValue($node)

    Local $ret_val3

    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val3) Then
        Return ($ret_val3[3])
    Else
        Return SetError(1,3,0)
    EndIf

EndFunc

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit :: 7.3.2011 6:50:09</title>
    <category title="System Overview">
        <subcategory title="Sub1">
            <recordset title="Titel01">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Computer Name</fieldvalue>
                    <fieldvalue>FOOUSER01</fieldvalue>
                </datarow>
                <datarow>
                    <fieldvalue>Domain Name</fieldvalue>
                    <fieldvalue>Workgroup</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
    <category title="Unseen data">
        <subcategory title="Sub2">
            <recordset title="Titel02">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Other Item</fieldvalue>
                    <fieldvalue>Other Value</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
</computeraudit>
Edited by neebo
Link to comment
Share on other sites

Try this, I added a few comments to the code.

#include "_XMLDomWrapper.au3"


$reportFile = @DesktopDir & "\CSV.txt"
$Folder = "c:\_diileri"

FileDelete($reportFile)
FileOpen($reportFile, 9)


; #########################################################

; search files

$search = FileFindFirstFile(@ScriptDir & '\*.xml')
MsgBox(0, "SEARCH INIT:", $search, 2)

If $search = -1 Then ; ERROR
    MsgBox(0, "ERROR", "FILE NOT FOUND", 3)
    Exit
EndIf

; #########################################################

$searchItem = "Computer name"
$searchValue = "Rav"

; ######################################################

While 1 ; search all xml files on the folder

    $file = FileFindNextFile($search)
    If @error then ExitLoop ;<< @error check needs to go before your msgbox or it will never be detected as the msgbox will return not an error!

    MsgBox(0, $file, $search, 2)

    _XMLFileOpen($file) ; xmlFile to be opened



    $iCatagoryCount = _XMLGetNodeCount("/computeraudit/category") ;Need to see how manay Catagory sections there are
    ConsoleWrite("iCatagoryCount = " & $iCatagoryCount & @crlf)

    For $iCat = 1 to $iCatagoryCount ;loop each catagory section

        $iDatarowCount = _XMLGetNodeCount("/computeraudit/category[" & $iCat & "]/subcategory/recordset/datarow")
        ConsoleWrite("iDatarowCount = " & $iDatarowCount & @crlf)

        For $iData = 1 to $iDatarowCount

            Local $aXMLValue = _XMLGetValue("/computeraudit/category[" & $iCat & "]/subcategory/recordset/datarow[" & $iData & "]/fieldvalue")
            If IsArray($aXMLValue) and $aXMLValue[0] >= 2 then
                $XMLitem = $aXMLValue[1]
                $XMLvalue = $aXMLValue[2]
                ConsoleWrite("XMLitem = " & $XMLitem & @crlf & "XMLvalue = " & $XMLvalue & @CRLF)
            EndIf

            $subSearchItem = StringInStr($XMLitem, $searchItem, 2)
            $subSearchValue = StringInStr($XMLvalue, $searchValue, 2)

            If $subSearchItem And $subSearchValue Then
                MsgBox(0, "HIT", $XMLitem & @CRLF & $XMLvalue, 2)
                FileWrite($reportFile, $file & "     " & "ITEM: " & $XMLitem & "     " & "VALUE: " & $XMLvalue & @CRLF) ; write data
                ExitLoop(1)

            EndIf

        Next;<< End of Datarow loop

    Next;<< End of Catagory Loop

    ;FileClose($file) ; Don't need this!

WEnd

FileClose($Search) ;<< The search handle needs closing not the $file one
FileClose($reportFile)

Exit

; #### Didn't need any of the functions below
Link to comment
Share on other sites

Thanks a lot!

Found propably one problem tho:

$SearchItem and $searchValue are found, if the are in

1st Category or

2st Category

but:

they are not found if in 2st Category/Subcategory or ...Recordset

Try running a search with "Talkshowhost" and "Leno" with following example files...

Also: some datarows contain three fieldvalues. Can the 3rd fieldvalue also be read as result?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit :: 18.3.2011 3:59:14</title>
    <category title="First">
        <subcategory title="">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Talkshowhost</fieldvalue>
                    <fieldvalue>Jay Leno</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
    <category title="Another Part">
        <subcategory title="Drive C">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Foo</fieldvalue>
                    <fieldvalue>Uncle Bob</fieldvalue>
                </datarow>          
            </recordset>
        </subcategory>
        <subcategory title="Drive D">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Foo</fieldvalue>
                    <fieldvalue>Greg Kinnear</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
</computeraudit>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit :: 18.3.2011 3:59:14</title>
    <category title="First">
        <subcategory title="">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Some Item</fieldvalue>
                    <fieldvalue>Some Value</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
    <category title="Another Part">
        <subcategory title="Drive C">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Talkshowhost</fieldvalue>
                    <fieldvalue>Jay Leno</fieldvalue>
                </datarow>          
            </recordset>
        </subcategory>
        <subcategory title="Drive D">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Foo</fieldvalue>
                    <fieldvalue>Greg Kinnear</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
</computeraudit>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit :: 18.3.2011 3:59:14</title>
    <category title="First">
        <subcategory title="">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Some Item</fieldvalue>
                    <fieldvalue>Some Value</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
    <category title="Another Part">
        <subcategory title="Drive C">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Actor</fieldvalue>
                    <fieldvalue>John Smith</fieldvalue>
                </datarow>          
            </recordset>
        </subcategory>
        <subcategory title="Drive D">
            <recordset title="">
                <fieldname>Item</fieldname>
                <fieldname>Value</fieldname>
                <datarow>
                    <fieldvalue>Talkshowhost</fieldvalue>
                    <fieldvalue>Jay Leno</fieldvalue>
                </datarow>
            </recordset>
        </subcategory>
    </category>
</computeraudit>
Edited by neebo
Link to comment
Share on other sites

I didn't realise there were multiple Sub Catagories!

#include "_XMLDomWrapper.au3"


$reportFile = @DesktopDir & "\CSV.txt"
$Folder = "c:\_diileri"

FileDelete($reportFile)
FileOpen($reportFile, 9)


; #########################################################

; search files

$search = FileFindFirstFile(@ScriptDir & '\*.xml')
MsgBox(0, "SEARCH INIT:", $search, 2)

If $search = -1 Then ; ERROR
    MsgBox(0, "ERROR", "FILE NOT FOUND", 3)
    Exit
EndIf

; #########################################################

$searchItem = "Talkshowhost"
$searchValue = "Leno"

; ######################################################

While 1 ; search all xml files on the folder

    $file = FileFindNextFile($search)
    If @error then ExitLoop ;<< @error check needs to go before your msgbox or it will never be detected as the msgbox will return not an error!

    MsgBox(0, $file, $search, 2)
    ConsoleWrite($file & @crlf)
    _XMLFileOpen($file) ; xmlFile to be opened



    $iCatagoryCount = _XMLGetNodeCount("/computeraudit/category") ;Need to see how manay Catagory sections there are
    ConsoleWrite("iCatagoryCount = " & $iCatagoryCount & @crlf)

    For $iCat = 1 to $iCatagoryCount ;loop each catagory section

        $iSubCatagoryCount = _XMLGetNodeCount("/computeraudit/category[" & $iCat & "]/subcategory")

            For $iSubCat = 1 to $iSubCatagoryCount

                $iDatarowCount = _XMLGetNodeCount("/computeraudit/category[" & $iCat & "]/subcategory[" & $iSubCat & "]/recordset/datarow")
                ConsoleWrite("iDatarowCount = " & $iDatarowCount & @crlf)

                    For $iData = 1 to $iDatarowCount

                        Local $aXMLValue = _XMLGetValue("/computeraudit/category[" & $iCat & "]/subcategory[" & $iSubCat & "]/recordset/datarow[" & $iData & "]/fieldvalue")
                        If IsArray($aXMLValue) and $aXMLValue[0] >= 2 then
                            $XMLitem = $aXMLValue[1]
                            $XMLvalue = $aXMLValue[2]
                            ConsoleWrite("XMLitem = " & $XMLitem & @crlf & "XMLvalue = " & $XMLvalue & @CRLF)
                        EndIf

                        $subSearchItem = StringInStr($XMLitem, $searchItem, 2)
                        $subSearchValue = StringInStr($XMLvalue, $searchValue, 2)

                        If $subSearchItem And $subSearchValue Then
                            MsgBox(0, "HIT", $XMLitem & @CRLF & $XMLvalue, 2)
                            FileWrite($reportFile, $file & "     " & "ITEM: " & $XMLitem & "     " & "VALUE: " & $XMLvalue & @CRLF) ; write data
                            ExitLoop(1)

                        EndIf

                    Next;<< End of Datarow loop

            Next ;<< End SubCatagory loop

    Next;<< End of Catagory Loop

    ;FileClose($file) ; Don't need this!

WEnd

FileClose($Search) ;<< The search handle needs closing not the $file one
FileClose($reportFile)

Exit

; #### Functions
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...