我有一个 LibreOffice Basic 宏来编写/读取 XML 文件。我已经从 VBA 移植了它们。到目前为止,一切似乎都在工作,除了从 XML 元素中检索文本。下面是创建 XML 对象、将其写入文件并将 XML 文件读入 XML 对象的(简化的)Sub:
Sub ExportImportXML
Dim oleService, oXMLexp as Object, oXMLimp as Object
Dim oRoot, oNode as Object
Dim HOME as String
oleService = createUnoService("com.sun.star.bridge.OleObjectFactory")
' Change this one:
HOME = "C:\tmp"
' Create an XML object to export
oXMLexp = oleService.createInstance("Msxml.DOMDocument")
With oXMLexp
.async = False
.validateOnParse = False
.resolveExternals = False
oRoot = .createElement("Root")
.appendChild(oRoot)
oNode = .createElement("ElementA")
oNode.Text = "Text of ElementA"
oRoot.appendChild(oNode)
MsgBox oNode.xml
'THIS DOESN'T WORK!
'MsgBox oNode.Text
oNode = .createElement("ElementB")
oNode.Text = "Text of ElementB"
oRoot.appendChild(oNode)
MsgBox .xml
.Save(HOME & "\test1.xml")
End With
' Create an XML object to import
oXMLimp = oleService.createInstance("Msxml.DOMDocument")
With oXMLimp
.Load(HOME & "\test1.xml")
MsgBox .xml
oNode = .DocumentElement.getElementsByTagName("ElementA").Item(0)
MsgBox oNode.xml
'THIS DOESN'T WORK!
'MsgBox oNode.Text
End With
End Sub
如您所见,我可以使用.Text写入节点,但无法从中读取。
顺便说一句,这在 VBA 中非常有效。
任何想法为什么会发生这种情况?