Is there any easy way to open/edit/save XML file? Any function which can open xml file into array? I tried _FileReadToArray($filepath, $aArray)
but this has wrong codding (I need utf-16, not ansi)
问题:
回答1:
There is also an "official" function named "_SML_ElementsToArry()"
;===============================================================================
;
; Function Name: _XML_ElementsToArry()
; Description: Return an array of the elements of an XML file
; Parameter(s): $sXMLFilePath - String with full path to XML file
; Requirement(s): AutoIt 3.1.1.53 Beta or better
; Return Value(s): On Success - Returns an array. Element[0] contains the
; number of elements found, the remaining
; array contains the names of the elements
; On Failure - returns -1 and sets @error to 1
; Author(s): JerryD
;
;===============================================================================
Func _XML_ElementsToArry ( $sXMLFilePath )
Local $i = 1
Local $aRetAry[1]
$aRetAry[0] = 0
Local $xmldoc = ObjCreate( 'Microsoft.XMLDOM' )
If Not IsObj($xmldoc) Then
SetError ( 1 )
Return -1
EndIf
$xmldoc.async = False
$xmldoc.load ( $sXMLFilePath )
For $x In $xmldoc.documentElement.childNodes
$aRetAry[0] = $aRetAry[0] + 1
ReDim $aRetAry[$aRetAry[0]+1]
$aRetAry[$i] = $x.NodeName
$i = $i + 1
Next
Return $aRetAry
EndFunc
Cheers, Vlu.
回答2:
You can use MSXML to properly read the XML file:
$oXml = ObjCreate('Msxml2.DOMDocument.3.0')
If IsObj($oXml) Then
$oXml.load('C:\Path\to\your\file.xml')
If $oXml.parseError.errorCode = 0 Then
; Do something with the XML object
Else
MsgBox(4096, 'Error', 'Error opening XML file: ' & _
$oXml.parseError.reason)
SetError($oXml.parseError.errorCode)
EndIf
EndIf
You can then use the MSXML API to build up your array.
回答3:
This code works for reading an XML file and loading it into a dynamically sized 2D array. It requires Eltorro's _MSXML.au3 UDF.
; Load _MSXML.au3 UDF found here:
; https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=44418
#include <_MSXML.au3>
; Set the XML file
$filename = @ScriptDir & "\Test_1.xml"
; Call the function
$array = XMLtoArray($filename)
Func XMLtoArray($XMLfile)
Local $oXMLDoc
_MSXML_InitInstance($oXMLDoc)
If @error Then
MsgBox(0,"Error","Failed to create instance")
Exit
EndIf
_MSXML_FileOpen($oXMLDoc, $XMLfile)
If @error Then
MsgBox(0,"Error",_MSXML_Error())
Else
; File open and ready.
local $resultArray[0][0]
; Create XML OBJECT
$oRecords = $oXMLDoc.documentElement.childNodes
; Create Array with headers in row 0
$length = $oRecords.item(0).attributes.length
ReDim $resultArray[1][$length]
$j = 0
For $name in $oRecords.item(0).attributes
$name = $oRecords.item(0).attributes($j).name
$resultArray[0][$j] = $name
$j+=1
Next
; Fill array with values
For $oRecord In $oRecords
ReDim $resultArray[UBound($resultArray, $UBOUND_ROWS)+1][UBound($resultArray, $UBOUND_COLUMNS)]
For $j = 0 To UBound($resultArray, $UBOUND_COLUMNS) - 1 Step +1
$attributeName = $resultArray[0][$j]
$attributeValue = $oRecord.GetAttribute($attributeName)
$resultArray[UBound($resultArray)-1][$j] = $attributeValue
Next
Next
Return $resultArray
EndIf
EndFunc
回答4:
there is a XML DOM Wrapper available for AutoIT.
Just have a look at:
http://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/
The Download Links on the first page are broken (Thread is a bit old ;), so try using these below:
http://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/page-38
https://raw.githubusercontent.com/Silvernine0S/FolderMenu3EX/master/Include/XMLDomWrapper.au3
I use the lib with AutoIT Version v3.3.8.1 (Seems to be working so far).