Using custom XmlContentHandler for reading QDomDocument
-
Hi, i want use custom XmlReader for reading in QDomDocument. But Dom ignoring my Handler.
@
#include <QtXml>
#include <QDebug>class MyHandler : public QXmlDefaultHandler
{
public:
virtual bool startElement ( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts );
};bool
MyHandler::startElement ( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
{
qDebug() << localName;
return true;
}int main()
{
MyHandler *handler = new MyHandler;
QFile file("simple.xml");
file.open(QIODevice::ReadOnly);
QXmlSimpleReader *reader = new QXmlSimpleReader;
reader->setContentHandler(handler);
reader->setErrorHandler(handler);
QXmlInputSource *source = new QXmlInputSource (&file);QDomDocument dom;
dom.setContent(source, reader);
file.close();
return 0;
}
@
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? -
add .pro file also..
-
domtest.pro:
@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .QT += xml
Input
SOURCES += domtest.cpp
@simple.xml (but you can use any valid xml):
@
<h1 a="123" b="345">
<h2 c="testing">
</h2>
</h1>
@