I have a LibreOffice Basic macros to write/ read XML files. I have ported them from VBA. So far everything seems to be working except for retrieving the text from XML elements. Here is the (simplified) Sub that creates an XML object, writes it to file and reads an XML file into an XML object:
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
As you can see, I can write to a Node using .Text but I cannot read from it.
By the way, this works perfectly in VBA.
Any ideas why this is happening?
I got it. For some reason the program doesn't recognize .Text property for a Node. It does, however, recognize .nodeTypedValue. The difference is that .Text property returns unformatted text of this node whereas .nodeTypedValue property returns a formatted value of the node.
More details at: http://msdn.microsoft.com/en-us/library/office/aa163921(v=office.10).aspx
So to make the code above work, substitute oNode.Text with oNode.nodeTypedValue.
Additional note: if you want to use a specific version of MSXML, you can create an XML object as following (without using OleObjectFactory):
oXML = CreateObject("Msxml2.DOMDocument.4.0")