Showing an approach based on XFastParser:
' A handler, needs to be visible to allow returning from createUnknownChildContext
Dim oDocumentHandler
' End results will be stored here
Dim bFoundFoo As Boolean
Dim bFoundBar As Boolean
Dim sFoundBarValue As String
Dim bFoundBaz As Boolean
' Implementation of (some) methods of XFastDocumentHandler
Function DocHandler_setDocumentLocator(xLocator)
End Function
Function DocHandler_startDocument()
End Function
Function DocHandler_endDocument()
End Function
Function DocHandler_createUnknownChildContext(Namespace, Name, Attribs)
DocHandler_createUnknownChildContext = oDocumentHandler
End Function
Function DocHandler_startUnknownElement(Namespace, Name, Attribs)
If Name = "Foo" Then bFoundFoo = True
unkAttribs = Attribs.getUnknownAttributes()
For Each a In unkAttribs
If a.Name = "Bar" Then
bFoundBar = True
sFoundBarValue = a.Value
End If
Next a
End Function
Function DocHandler_endUnknownElement(Namespace, Name)
End Function
Function DocHandler_characters(aChars)
If aChars = "Baz" Then bFoundBaz = True
End Function
' Sample function processing a file
Sub Parse
source = CreateUnoStruct("com.sun.star.xml.sax.InputSource")
source.aInputStream = CreateUnoService("com.sun.star.ucb.SimpleFileAccess").openFileRead(ConvertToURL("path/to/file.xml"))
parser = CreateUnoService("com.sun.star.xml.sax.FastParser")
oDocumentHandler = createUnoListener("DocHandler_", "com.sun.star.xml.sax.XFastDocumentHandler")
parser.setFastDocumentHandler(oDocumentHandler)
parser.ParseStream(source)
MsgBox bFoundFoo & " " & bFoundBar & " " & bFoundBaz & "; Bar was " & sFoundBarValue
End Sub
Given the code, and this path/to/file.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<Foo/>
<Something Bar="Some Value"/>
<SomethingElse>Baz</SomethingElse>
</xml>
the result of running Parse
will be a message box with “True True True; Bar was Some Value”.
Indeed, this all may be also implemented in another scripting language (in anticipation of “use the real programming language!!!” ) - but the technique of implementing a given UNO interface in Basic may be useful for some by itself.