Difference between revisions of "XML and Filter"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
m (Parsing a XML File with SAX)
Line 3: Line 3:
 
To start, have a look at :
 
To start, have a look at :
  
Using UNO's Xml sax parser via the API http://www.oooforum.org/forum/viewtopic.phtml?t=4907
+
[ http://www.oooforum.org/forum/viewtopic.phtml?t=4907 Using UNO's Xml sax parser via the API ]
 
   
 
   
It's a Danny Brewer's OooBasic program which use SAX. To avoid searching I begin to give the code here.
+
It's a [[CppSDKAuthors|Danny Brewer's]] OooBasic program which use SAX. To avoid searching I begin to give the code here.
 
Sax and OooBasic (Danny Brewer)
 
Sax and OooBasic (Danny Brewer)
  
Line 19: Line 19:
  
 
OOo has a Sax Xml parser available via. the Uno api. The following program, in Basic, shows how to use it to parse an Xml document. As the document is parsed, events are fired which print little annoying dialog boxes on the screen. (Be sure to parse a VERY SMALL xml document so that you only have to click OK about a dozen or so times!)  
 
OOo has a Sax Xml parser available via. the Uno api. The following program, in Basic, shows how to use it to parse an Xml document. As the document is parsed, events are fired which print little annoying dialog boxes on the screen. (Be sure to parse a VERY SMALL xml document so that you only have to click OK about a dozen or so times!)  
<code>[OOBas]
+
<code>[OOOBas]
 
Listing 1 Using SAX with OooBasic
 
Listing 1 Using SAX with OooBasic
 
REM  *****  BASIC  *****
 
REM  *****  BASIC  *****

Revision as of 19:27, 31 August 2006

We want in this chapter examine how XML and filters work.

Parsing a XML File with SAX

To start, have a look at :

[ http://www.oooforum.org/forum/viewtopic.phtml?t=4907 Using UNO's Xml sax parser via the API ]

It's a Danny Brewer's OooBasic program which use SAX. To avoid searching I begin to give the code here. Sax and OooBasic (Danny Brewer)

This example demonstrates several things.

  • Using the UCB's SimpleFileAccess to read from a file. (UCB = Universal Content Broker)
  • Using OOo's XML sax parser.
  • Creating a listener with Basic's CreateUnoListener() function.

If you have never used a Sax type of Xml parser before, then this example may not be for you.

OOo has a Sax Xml parser available via. the Uno api. The following program, in Basic, shows how to use it to parse an Xml document. As the document is parsed, events are fired which print little annoying dialog boxes on the screen. (Be sure to parse a VERY SMALL xml document so that you only have to click OK about a dozen or so times!) [OOOBas] Listing 1 Using SAX with OooBasic REM ***** BASIC ***** REM **** Danny Brewer (Mon Jan 12, 2004) **** Sub Main

 cXmlFile = "C:\TestData.xml"  
 cXmlUrl = ConvertToURL( cXmlFile ) 
 ReadXmlFromUrl( cXmlUrl ) 

End Sub

' This routine demonstrates how to use the Universal Content Broker's ' SimpleFileAccess to read from a local file. Sub ReadXmlFromUrl( cUrl ) ' The SimpleFileAccess service provides mechanisms to open, read, write files, ' as well as scan the directories of folders to see what they contain. ' The advantage of this over Basic's ugly file manipulation is that this ' technique works the same way in any programming language. ' Furthermore, the program could be running on one machine, while the SimpleFileAccess ' accesses files from the point of view of the machine running OOo, not the machine ' where, say a remote Java or Python program is running.

 oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )  

' Open input file.

 oInputStream = oSimpleFileAccess.openFileRead( cUrl ) 

ReadXmlFromInputStream( oInputStream )

 oInputStream.closeInput() 

End Sub

Sub ReadXmlFromInputStream( oInputStream ) ' Create a Sax Xml parser.

 oSaxParser = createUnoService( "com.sun.star.xml.sax.Parser" ) 

' Create a document event handler object. ' As methods of this object are called, Basic arranges ' for global routines (see below) to be called.

 oDocEventsHandler = CreateDocumentHandler() 

' Plug our event handler into the parser. ' As the parser reads an Xml document, it calls methods ' of the object, and hence global subroutines below ' to notify them of what it is seeing within the Xml document.

 oSaxParser.setDocumentHandler( oDocEventsHandler ) 

' Create an InputSource structure.

 oInputSource = createUnoStruct( "com.sun.star.xml.sax.InputSource" ) 
 With oInputSource 
   .aInputStream = oInputStream ' plug in the input stream 
 End With 

' Now parse the document. ' This reads in the entire document. ' Methods of the oDocEventsHandler object are called as ' the document is scanned.

 oSaxParser.parseStream( oInputSource ) 

End Sub

'================================================== ' Xml Sax document handler. '================================================== ' Global variables used by our document handler. ' ' Once the Sax parser has given us a document locator, ' the glLocatorSet variable is set to True, ' and the goLocator contains the locator object. ' ' The methods of the locator object has cool methods ' which can tell you where within the current Xml document ' being parsed that the current Sax event occured. ' The locator object implements com.sun.star.xml.sax.XLocator. ' Private goLocator As Object Private glLocatorSet As Boolean

' This creates an object which implements the interface ' com.sun.star.xml.sax.XDocumentHandler. ' The doucment handler is returned as the function result. Function CreateDocumentHandler() ' Use the CreateUnoListener function of Basic. ' Basic creates and returns an object that implements a particular interface. ' When methods of that object are called, ' Basic will call global Basic functions whose names are the same ' as the methods, but prefixed with a certian prefix.

 oDocHandler = CreateUnoListener( "DocHandler_", "com.sun.star.xml.sax.XDocumentHandler" ) 
 glLocatorSet = False 
 CreateDocumentHandler() = oDocHandler 

End Function

'================================================== ' Methods of our document handler call these ' global functions. ' These methods look strangely similar to ' a SAX event handler. ;-) ' These global routines are called by the Sax parser ' as it reads in an XML document. ' These subroutines must be named with a prefix that is ' followed by the event name of the com.sun.star.xml.sax.XDocumentHandler interface. '================================================== Sub DocHandler_startDocument()

 Print "Start document" 

End Sub

Sub DocHandler_endDocument() ' Print "End document" End Sub

Sub DocHandler_startElement( cName As String, oAttributes As com.sun.star.xml.sax.XAttributeList )

 Print "Start element", cName 

End Sub

Sub DocHandler_endElement( cName As String ) ' Print "End element", cName End Sub

Sub DocHandler_characters( cChars As String ) End Sub

Sub DocHandler_ignorableWhitespace( cWhitespace As String ) End Sub

Sub DocHandler_processingInstruction( cTarget As String, cData As String ) End Sub

Sub DocHandler_setDocumentLocator( oLocator As com.sun.star.xml.sax.XLocator ) ' Save the locator object in a global variable. ' The locator object has valuable methods that we can ' call to determine

 goLocator = oLocator 
 glLocatorSet = True 

End Sub

This code install an eventListener with a com.sun.star.xml.sax.XDocumentHandler interface.The corresponding IDL documentation of this interface is :

Personal tools