Ask Your Question
0

How to get an XML element text in a LibreOffice Basic macro? [closed]

asked 2013-11-30 16:35:06 +0200

sargdavid gravatar image

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?

Thank you in advance!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2015-11-15 07:34:56.924944

1 Answer

Sort by » oldest newest most voted
0

answered 2013-12-01 05:29:54 +0200

sargdavid gravatar image

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")
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-11-30 16:35:06 +0200

Seen: 1,238 times

Last updated: Dec 01 '13