June 15, 2011

lexa_ lexa_
Lab Rat
2 posts

Using custom XmlContentHandler for reading QDomDocument

 

Hi, i want use custom XmlReader for reading in QDomDocument. But Dom ignoring my Handler.

  1. #include <QtXml>
  2. #include <QDebug>
  3.  
  4.  
  5. class MyHandler : public QXmlDefaultHandler
  6. {
  7. public:
  8.   virtual bool startElement ( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts );
  9. };
  10.  
  11. bool
  12. MyHandler::startElement ( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
  13. {
  14.  qDebug() << localName;
  15.  return true;
  16. }
  17.  
  18. int main()
  19. {
  20.  MyHandler *handler = new MyHandler;
  21.  QFile file("simple.xml");
  22.  file.open(QIODevice::ReadOnly);
  23.  QXmlSimpleReader *reader = new QXmlSimpleReader;
  24.  reader->setContentHandler(handler);
  25.  reader->setErrorHandler(handler);
  26.  QXmlInputSource *source = new QXmlInputSource (&file);
  27.  
  28.  dom.setContent(source, reader);
  29.  file.close();
  30.  return 0;
  31. }

This code is reading file simple.xml and create DOM object. But it don’t using MyHandler class for parsing. The function MyHandler::startElement is not called. Why this happened?

2 replies

June 15, 2011

TobbY TobbY
Lab Rat
208 posts

add .pro file also..

 Signature 

Gorilla..

June 15, 2011

lexa_ lexa_
Lab Rat
2 posts

domtest.pro:

  1. TEMPLATE = app
  2. TARGET =
  3. DEPENDPATH += .
  4. INCLUDEPATH += .
  5.  
  6. QT += xml
  7.  
  8. # Input
  9. SOURCES += domtest.cpp

simple.xml (but you can use any valid xml):

  1. <h1 a="123" b="345">
  2. <h2 c="testing">
  3. </h2>
  4. </h1>

 
  ‹‹ [SOLVED] [QColumnView] Populate widget using for loops and placeholders in PostgreSQL queries      [Moved] Is anyone tried gSoap on qt. ››

You must log in to post a reply. Not a member yet? Register here!