我想让AutoIt使用Windows API与我系统上的XML文件进行交互。
我设法很好地加载和读取XML文件,但我在写入它时遇到了一些问题。
AutoIt脚本
; An object for the XML-file on Windows OS using Microsoft's API.
; @see https://msdn.microsoft.com/en-us/library/ms757828(v=vs.85).aspx
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.async = False
; Check to see if the XML-file is in place.
; @see https://www.autoitscript.com/autoit3/docs/functions/FileExists.htm
If FileExists(@ScriptDir & "\" & "db2.xml") Then
; Load the proper XML-file to the XML-object.
; @see https://msdn.microsoft.com/en-us/library/ms762722(v=vs.85).aspx
$oXML.load(@ScriptDir & "\" & "db2.xml")
Else
; Create a new XML-file.
If Not _FileCreate(@ScriptDir & "\" & "db2.xml") Then
MsgBox(16, "Fatal Error", "Could neither load nor create XML file!")
Exit
Else
; Load the newly created XML-file to the XML-object.
; @see https://msdn.microsoft.com/en-us/library/ms762722(v=vs.85).aspx
$oXML.load(@ScriptDir & "\" & "db2.xml")
EndIf
EndIf
; Create a node to be written to the XML-object.
; @see https://msdn.microsoft.com/en-us/library/ms757901(v=vs.85).aspx
$current = $oXML.createNode(8, "test", "")
; Save the XML-object to the XML-document.
; @see https://msdn.microsoft.com/en-us/library/ms753769(v=vs.85).aspx
$oXML.save(@ScriptDir & "\" & "db2.xml")我的XML-file位于脚本目录中,完全为空。
当我运行AutoIt脚本时,我绝对没有收到任何错误消息
当将焦点改变到加载到Notepad++中的XML文档时,
我收到该文件最近已被修改的消息
由一个外部程序,它询问我是否要重新加载它。
我知道,但文件看起来仍然是空的。
根据我的逻辑,文件中应该有一个来自createNode命令的注释。
发布于 2017-05-29 17:55:56
如果您想添加注释,请使用createComment函数tho。您必须首先获取元素,然后在元素上追加,而不是在上对XML-Object执行此操作。
test.xml
<test>
</test>xml.au3
Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load(@ScriptDir & "\test.xml")
ConsoleWrite("$oXml.ReadyState = " & $oXml.ReadyState & @LF)
Local $oRootElement = $oXML.documentElement
Local $oNewElement = $oXML.createElement("NEW")
$oRootElement.appendChild($oNewElement)
$oXML.save(@ScriptDir & "\test.xml")
oRootElement = ""
$oRootElement = $oXML.documentElement
Local $oComment = $oXML.createComment("test")
$oRootElement.appendChild($oComment)
$oXML.save(@ScriptDir & "\test.xml")
; User's COM error function. Will be called if COM error occurs
; Source: https://www.autoitscript.com/forum/topic/170063-how-to-use-selectnodes-on-xmldom/
Func _ErrFunc($oError)
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc ;==>_ErrFunc更新:-添加了createElement示例。
https://stackoverflow.com/questions/44206555
复制相似问题