Jump to content

XML with AutoIT (_XMLDomWrapper.au3)


Recommended Posts

Hi,

i have the following xml file:

<DirectoryRef Id="FIRST">
  <Component Id="X" Guid="{XX}">
    <File Id="base.dll" Source="XXX"/>
    <File Id="x86.dll" Source="BBBB"/>
  </Component>

  <Component Id="Y" Guid="{YY}">
    <File Id="base2.dll" Source="YYY"/>
    <File Id="x862.dll" Source="CCC"/>
  </Component>
</DirectoryRef>

<DirectoryRef Id="Second">
  <Component Id="X" Guid="{XX}">
    <File Id="base.dll" Source="XXX"/>
    <File Id="x86.dll" Source="BBBB"/>
  </Component>

  <Component Id="Y" Guid="{YY}">
    <File Id="base2.dll" Source="YYY"/>
    <File Id="x862.dll" Source="CCC"/>
  </Component>
</DirectoryRef>

how can i get with _XMLDomWrapper.au3 the 'File ID' in 2 txt file --> txt files should be have in my Case:

1.txt file (for DirectoryRef Id="FIRST"):

------------------------------------------

base.dll

x86.dll

base2.dll

x862.dll

2.txt file (for DirectoryRef Id="Second"):

------------------------------------------

base.dll

x86.dll

base2.dll

x862.dll

10x :-)

Link to comment
Share on other sites

but i want this with XMLDomWrapper . I start to write my Code. It is like this;

#include <Array.au3>
#include <_XMLDOMWrapper.au3>
  $file = FileOpen("test.txt", 1)
$XML = FileOpenDialog("", @ProgramFilesDir & "\autoit", "XML (*.xml)", 1)
$XMLopen = _XMLFileOpen($XML)
ConsoleWrite("Debug: $iRET = " & $XMLopen & @LF)
$XMLcount = _XmlGetNodeCount("AutoitShare/Script")

For $i = 1 To $XMLcount
.
.
.
Next

any Idee how can i do my FOR-Loop ?

Regards

Link to comment
Share on other sites

First, you don't have a valid XML file because it has no root node:

<?xml version="1.0"?>
<root>
<DirectoryRef Id="FIRST">
  <Component Id="X" Guid="{XX}">
   <File Id="base.dll" Source="XXX"/>
   <File Id="x86.dll" Source="BBBB"/>
  </Component>
  <Component Id="Y" Guid="{YY}">
   <File Id="base2.dll" Source="YYY"/>
   <File Id="x862.dll" Source="CCC"/>
  </Component>
</DirectoryRef>
<DirectoryRef Id="Second">
  <Component Id="X" Guid="{XX}">
   <File Id="base.dll" Source="XXX"/>
   <File Id="x86.dll" Source="BBBB"/>
  </Component>
  <Component Id="Y" Guid="{YY}">
   <File Id="base2.dll" Source="YYY"/>
   <File Id="x862.dll" Source="CCC"/>
  </Component>
</DirectoryRef>
</root>

Second, where did you get "AutoItShare/Scrpt" from? There is no such path in the XML given.

This would be one way to enumerate the attributes:

#include <_XMLDOMWrapper.au3>
$sXml = @ScriptDir & "\Test1.xml"
$XMLopen = _XMLFileOpen($sXML)
$iDirRefCount = _XMLGetNodeCount("/root/DirectoryRef")
For $n = 1 To $iDirRefCount
$sDirRefID = _XMLGetAttrib("/root/DirectoryRef[" & $n & "]", "Id")
ConsoleWrite("$sDirRefID = " & $sDirRefID & @LF)
$iCompCount = _XMLGetNodeCount("/root/DirectoryRef[" & $n & "]/Component")
For $c = 1 To $iCompCount
  $sCompID = _XMLGetAttrib("/root/DirectoryRef[" & $n & "]/Component[" & $c & "]", "Id")
  ConsoleWrite(@TAB & "$sCompID = " & $sCompID & @LF)
  $iFileCount = _XMLGetNodeCount("/root/DirectoryRef[" & $n & "]/Component[" & $c & "]/File")
  For $f = 1 To $iFileCount
   $sFileID = _XMLGetAttrib("/root/DirectoryRef[" & $n & "]/Component[" & $c & "]/File[" & $f & "]", "Id")
   $sFileSource = _XMLGetAttrib("/root/DirectoryRef[" & $n & "]/Component[" & $c & "]/File[" & $f & "]", "Source")
   ConsoleWrite(@TAB & @TAB & "$sFileID = " & $sFileID & "; $sFileSource = " & $sFileSource & @LF)
  Next
Next
Next

:mellow:

P.S. The new code tags really bite, but you knew that...

Edited by PsaltyDS
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

Just for comparision here is derived version using StringRegExp()

$XMLtext = FileRead(@ScriptDir & "\Test1.xml")
$DirRef = GetNode($XMLtext, 'DirectoryRef')
For $i = 0 To UBound($DirRef) - 1
    $sDirRefID = GetAttrib($DirRef[$i], 'DirectoryRef', 'Id')
    ConsoleWrite("$sDirRefID = " & $sDirRefID & @CRLF)
 
    $Comp = GetNode($DirRef[$i], 'Component')
    For $j = 0 To UBound($Comp) - 1
        $sCompID = GetAttrib($Comp[$j], 'Component', 'Id')
        ConsoleWrite(@TAB & "$sCompID = " & $sCompID & @CRLF)
        
        $File = GetNode2($Comp[$j], 'File')
        For $k = 0 To UBound($File) - 1
            $sFileID = GetAttrib($File[$k], 'File', 'Id')
            $sFileSource = GetAttrib($File[$k], 'File', 'Source')
            ConsoleWrite(@TAB & @TAB & "$sFileID = " & $sFileID & "; $sFileSource = " & $sFileSource & @CRLF)
        Next
    Next
Next
 
; <node ...> ... </node>
Func GetNode($xml, $node)
    Return StringRegExp($xml, '(?s)(<' & $node & '.*?>.*?</' & $node & '>)', 3)
EndFunc
 
; <node ... />
Func GetNode2($xml, $node)
    Return StringRegExp($xml, '(?s)(<' & $node & '.*?/>)', 3)
EndFunc
 
; <node ... attrib="xxx" ...>
Func GetAttrib($xml, $node, $attrib)
    $result = StringRegExp($xml, '(?i)<' & $node & '.*?' & $attrib & '="(.*?)".*?>', 3)
    If Not @error Then Return $result[0]
    Return ''
EndFunc

EDIT: @PsaltyDS

You can add Autoit tags manually like this [ autoit ] [ /autoit ] then code is not corrupted.

Edited by Zedna
Link to comment
Share on other sites

I don't like the XMLDomWrapper, so I use the COM-object directly. I think, this is better structured.

Global Const $oCOMError = ObjEvent("Autoit.Error", "_COMError")
Func _COMError()
    ConsoleWrite("! COM Error: " & $oCOMError.number & @LF)
EndFunc

$oXML = ObjCreate("MSXML2.DOMDocument")
;~ $oXML.loadXML(ClipGet())
$oXML.load(@ScriptDir & "\Test1.xml")
$oXML.setProperty("SelectionLanguage", "XPath")

ConsoleWrite("!-----------------------------------------------------------------------------------" & @LF & @LF)
For $oDirectoryRef In $oXML.selectNodes("/root/DirectoryRef")
    $sDirRefID = $oDirectoryRef.getAttribute("Id")
    ConsoleWrite("> Directory " & $sDirRefID & @LF)
    For $oComponent In $oDirectoryRef.selectNodes("Component")
        $sCompID = $oComponent.getAttribute("Id")
        ConsoleWrite("++> Component " & $sCompID & @LF)
        For $oFile In $oComponent.selectNodes("File")
            $sFileID = $oFile.getAttribute("Id")
            $sFileSource = $oFile.getAttribute("Source")
            ConsoleWrite("----> File " & $sFileID & " : " & $sFileSource & @LF)
        Next
    Next
    ConsoleWrite(@LF)
Next
ConsoleWrite("!-----------------------------------------------------------------------------------" & @LF & @LF)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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