有没有简单的方法来打开/编辑/保存XML文件?有什么函数可以将xml文件打开到数组中吗?我试过_FileReadToArray($filepath, $aArray)
但这有错误的溺爱(我需要utf-16,而不是ansi
还有一个名为"_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
干杯弗卢。
您可以使用 MSXML 正确读取 XML 文件:
$oXml = ObjCreate('Msxml2.DOMDocument.3.0')
If IsObj($oXml) Then
$oXml.load('C:Pathtoyourfile.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
然后,可以使用 MSXML API 构建阵列。
此代码用于读取 XML 文件并将其加载到动态大小的 2D 数组中。它需要Eltorro的_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
可用于AutoIT的XML DOM包装器。
看看:
http://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/
第一页上的下载链接已损坏(线程有点旧;),因此请尝试使用以下链接:
http://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/page-38
https://raw.githubusercontent.com/Silvernine0S/FolderMenu3EX/master/Include/XMLDomWrapper.au3
我将库与AutoIT版本v3.3.8.1一起使用(到目前为止似乎可以工作)。