OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Using UNO's Xml sax parser via the API in C++

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Aug 25, 2006 7:38 am    Post subject: Using UNO's Xml sax parser via the API in C++ Reply with quote

I have ported the DannyB OOoBasic thread in C++.

To start, have a look at

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

This code install an eventListener. If you want to see how it works in C++ have a look at

http://wiki.services.openoffice.org/wiki/OpenOffice_Calc#Event_Listener

We have first to create a C++ class :

Code:

class XFlatXml : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XDocumentHandler>
{         
private:
  Reference< XMultiServiceFactory > xMSF;
public:

  XFlatXml( const Reference< XMultiServiceFactory > &r ) :      xMSF( r )
      {}

//    Reference < com::sun::star::io::XOutputStream > xOutputStream;
    virtual void SAL_CALL startDocument() throw (SAXException,RuntimeException) ; 
    virtual void SAL_CALL endDocument() throw (SAXException,RuntimeException);
    virtual void SAL_CALL startElement(const OUString& str, const Reference<XAttributeList>& attriblist) throw (SAXException,RuntimeException);
    virtual void SAL_CALL endElement(const OUString& str)  throw (SAXException,RuntimeException);
    virtual void SAL_CALL characters(const OUString& str)  throw (SAXException,RuntimeException);
    virtual void SAL_CALL ignorableWhitespace(const OUString& str) throw (SAXException,RuntimeException);
    virtual void  SAL_CALL processingInstruction(const OUString& str, const OUString& str2) throw (SAXException,RuntimeException) ;
    virtual void SAL_CALL setDocumentLocator(const Reference<XLocator>& doclocator) throw (SAXException,RuntimeException) ;   
};


The corresponding implementation is :

Code:

void XFlatXml::startDocument() throw (SAXException,RuntimeException){
  printf("StartDocument\n");
}

void XFlatXml::endDocument() throw (SAXException,RuntimeException){
    printf("EndDocument\n");
}

void XFlatXml::startElement(const OUString& str, const Reference<XAttributeList>& attriblist) throw (SAXException,RuntimeException){
  printf("StartElement\n");
}

void XFlatXml::endElement(const OUString& str) throw (SAXException,RuntimeException) {
  printf("EndElement\n");
}

void XFlatXml::characters(const OUString& str) throw (SAXException,RuntimeException) {
  printf("characers\n");
}

void XFlatXml::ignorableWhitespace(const OUString& str) throw (SAXException,RuntimeException){
  printf("ignorableWhitespace\n");
}
 
void  XFlatXml::processingInstruction(const OUString& str, const OUString& str2) throw (SAXException,RuntimeException) {
  printf("processingInstruction\n");

}

void XFlatXml::setDocumentLocator(const Reference<XLocator>& doclocator) throw (SAXException,RuntimeException) {
    printf("setDocumentLocator\n");
}


which, as you can see only print out something in the shell.

We can install the listener and use it with such main program :

Code:

int main( )
{
//retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager;
    rOfficeServiceManager = ooConnect();
    OSL_ENSURE(rOfficeServiceManager.is(), "Unable to connected to the office\n");

// Installing our new XDocumentHandler
    XFlatXml *xListener = new XFlatXml(rOfficeServiceManager);
    Reference< XDocumentHandler > xHandler = static_cast< XDocumentHandler* > ( xListener );
// getting oSimpleFileAcess
  // com.sun.star.ucb.XSimpleFileAccess \ added in makefile
  // #include <com/sun/star/ucb/XSimpleFileAccess.hpp> added in this file
  // using namespace com::sun::star::ucb; added in this file
    Reference< XSimpleFileAccess > xSFI( rOfficeServiceManager->createInstance
           ( OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );   
    OSL_ENSURE(xSFI.is(), "Unable to get SimpleFileAcessService\n");

    OUString sUrl = OUString::createFromAscii("/home/smoutou/TestData.xml");
// getting oInputStream
  // using namespace com::sun::star::io; added in this file
    Reference <XInputStream > oInputStream=xSFI->openFileRead(sUrl);

// oSaxParser = createUnoService( "com.sun.star.xml.sax.Parser" )
  // com.sun.star.xml.sax.XParser \ added in Makefile
  //#include <com/sun/star/xml/sax/XParser.hpp> added in this file
    Reference < XParser > oSaxParser( rOfficeServiceManager->createInstance
           ( OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ) ), UNO_QUERY );   
    OSL_ENSURE(oSaxParser.is(), "Unable to get Sax Parser\n");
    oSaxParser->setDocumentHandler( xHandler );

  // com.sun.star.xml.sax.InputSource \ added in Makefile
  // #include <com/sun/star/xml/sax/InputSource.hpp> added in this file
    struct InputSource oInputSource;
    oInputSource.aInputStream = oInputStream;

    oSaxParser->parseStream(oInputSource);
    oInputStream->closeInput();
  return 0;
}


I have already given the ooConnect() program elsewhere : For instance here :
http://wiki.services.openoffice.org/wiki/UNO_automation_with_a_binary_%28executable%29#Preparing_a_new_Code_as_a_starting_Point

If you take this code and the file provided by danny, here is what you see in the shell :

Code:

setDocumentLocator
StartDocument
StartElement
characers
characers
StartElement
...

StartElement
characers
EndElement
characers
characers
EndElement
characers
characers
StartElement
characers
EndElement
characers
characers
EndElement
characers
EndElement
EndDocument


which shows us that we are on the right track.

I will work around XML for a while because I am a beginner in this subject. For now on, I cannot spend so much time because I return at work.
Hope to improve this code.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Sun Aug 27, 2006 8:29 am    Post subject: Reply with quote

First improvment the listener :
Code:

void XFlatXml::startElement(const OUString& str, const Reference<XAttributeList>& attriblist) throw (SAXException,RuntimeException){
  OString   OStr  = OUStringToOString ( str,RTL_TEXTENCODING_UTF8);
  cout<< "StartElement : <" << OStr << " ";
  for (short i=0;i<attriblist->getLength();i++){
    OStr  = OUStringToOString ( attriblist->getNameByIndex(i),RTL_TEXTENCODING_UTF8);
    cout << OStr <<"=";
    OStr  = OUStringToOString ( attriblist->getValueByIndex(i),RTL_TEXTENCODING_UTF8);
    cout << OStr;
  }
  cout << ">" << endl;
}

void XFlatXml::endElement(const OUString& str) throw (SAXException,RuntimeException) {
  OString   OStr  = OUStringToOString ( str,RTL_TEXTENCODING_UTF8);
  cout<< "EndElement : </" << OStr << ">" << endl;
 
}

void XFlatXml::characters(const OUString& str) throw (SAXException,RuntimeException) {
  OString   OStr  = OUStringToOString ( str,RTL_TEXTENCODING_UTF8);
  cout<< "Characers : " << OStr << endl;
}



which prints out :

Code:

setDocumentLocator
StartDocument
StartElement : <Employees >
Characers :
 
Characers :
StartElement : <Employee id=101>
Characers :
 
Characers :
StartElement : <Name >
Characers :
 
Characers :
StartElement : <First >
Characers : John
EndElement : </First>
Characers :
 
Characers :
StartElement : <Last >
Characers : Smith
EndElement : </Last>
Characers :
 
Characers :
.....

_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group