In VBA, I use the following function to simplify the process of reading an XML file into an MSXML2.DOMDocument60 object:
Public Function GetXMLFromFile2(strFilePath As String) As MSXML2.DOMDocument60
' ----------------------------------------------------------------
' Procedure Name: GetXML
' Purpose: Get XML from file into an MSXML2.DOMDocument60
' Procedure Kind: Function
' Procedure Access: Public
' Author: USER002
' Date: 06/12/2021
' ----------------------------------------------------------------
' Declarations
Dim objDOMDoc As New MSXML2.DOMDocument60
' Set up error handler
On Error GoTo ErrHandler
' Set up basic options for loading the XML file into objDOMDoc
With objDOMDoc
.async = False
.validateOnParse = False
.preserveWhiteSpace = True
End With
' Load the XML from input.xliff into objDOMDoc
objDOMDoc.Load (strFilePath)
Set GetXMLFromFile = objDOMDoc
ExitHere:
'On Error GoTo 0
Exit Function
ErrHandler:
Debug.Print Err.Number, Err.Description
Select Case Err.Number
Case 12345
'Do something
Case Else
End Select
End Function
In LO Basic, I have tried to recreate this function as follows:
Function GetXMLFromFile(strFilePath As String) As Object
'Declarations
'Dim objDOMDoc As New MSXML2.DOMDocument60
Dim objDOMDoc As Object
Dim objOLEService As Object
'Create UNO service
objOLEService = createUnoService("com.sun.star.bridge.OleObjectFactory")
' Create an XML object to export
objDOMDoc = objOLEService.createInstance("Msxml.DOMDocument")
'Get the XML document
objDOMDoc.Load("C:tmp\test1.xml")
'Return the XML document
GetXMLFromFile = objDOMDoc
End Function
I test the function like this:
Sub TestFunction
'Declarations
Dim objDOMDoc As Object
Dim objOLEService As Object
Dim oNode As Object
'Create UNO service
objOLEService = createUnoService("com.sun.star.bridge.OleObjectFactory")
' Create an XML object to export
objDOMDoc = objOLEService.createInstance("Msxml.DOMDocument")
objDOMDoc = GetXMLFromFile
oNode = objDOMDoc.DocumentElement.getElementsByTagName("ElementA").Item(0)
MsgBox oNode.xml
End Sub
Regrettably this does not work, and I have no idea why. When it hits the line
oNode = objDOMDoc.DocumentElement.getElementsByTagName("ElementA").Item(0)
I get the following error message:
This indicates to me that the function is not returning the XML object, or that the the sub is not able to read the XML object into its own object variable.
Regrettably, I have not been able to find a way to inspect the contents of the object variable objDOMDoc to see if it actually contains an XML object.
Any help with this would be greatly appreciated.