es geht auch mit iterator durch das ganze file runter rekursiv ... mit wenig schreibarbeit!
ich benutze so was um alle paragraf auszulesen von einem xhtml file... ohne nur eine regular expression zu schreiben.... dann habe ich den puren text aus dem html
Code: Alles auswählen
QDomDocument doc;
doc.setContent(XMLSTREAM);
DomElementContainer c(doc, "tag");
foreach(QDomElement e, c) {
if (e.tagName() == "tagnamex" ) {
qDebug("%s", qPrintable(e.text()));
}
}
Code: Alles auswählen
class DomElementContainer {
public:
class const_iterator {
friend class DomElementContainer;
public:
const_iterator(){}
const_iterator &operator++(){
m_e = m_e.nextSiblingElement(m_t);
return *this;
}
const QDomElement & operator*(){
return m_e;
}
bool operator==(const const_iterator &other) const {
return (m_e==other.m_e && m_t==other.m_t);
}
bool operator!=(const const_iterator &other) const {
return (m_e!=other.m_e || m_t!=other.m_t);
}
private:
const_iterator(const QDomElement &e, const QString &t){
m_e = e;
m_t = t;
}
QDomElement m_e;
QString m_t;
};
DomElementContainer(const QDomElement &e, const QString &tag=QString::null){
m_tag = tag;
m_elem = e;
}
DomElementContainer(const QDomDocument &d, const QString &tag=QString::null){
m_tag = tag;
m_elem = d.documentElement();
}
const_iterator begin() const{
return const_iterator(m_elem.firstChildElement(m_tag), m_tag);
}
const_iterator end() const {
return const_iterator(QDomElement(), m_tag);
}
private:
QDomElement m_elem;
QString m_tag;
};
qxml.h
Code: Alles auswählen
#ifndef QXML_H
#define QXML_H
/* save this file as qxml.h dont forget to set QT += xml on your pro file */
#include <QDomDocument>
#include <QtDebug>
#include <QDebug>
#include <QSettings>
#include <QDomDocument>
#include <QDomElement>
#include <QDomImplementation>
#include <QDomProcessingInstruction>
#include <QFile>
#include <QTextCodec>
#include <QString>
/*
sample read :
Qxml doc;
doc.SetSelf( const QString fullFileName );
QDomElement root = doc.root();
new doc
Qxml doc;
doc.SetstreamFile( QString sfile );
QDomElement root = doc.root();
append to root
#include "domelementcontainer.h"
const char *xml = "<test><tag>content 1</tag><tag>content 2</tag> "
"<othertag>wrongcontent</othertag>"
"<tag>content 3</tag></test>";
int main(){
QDomDocument doc;
doc.setContent(XMLSTREAM);
DomElementContainer c(doc, "tag");
foreach(QDomElement e, c)
qDebug("%s", qPrintable(e.text()));
return 0;
}
from http://www.qtcentre.org/ amministrator;
http://blog.wysota.eu.org/ http://www.wysota.eu.org/
*/
class DomElementContainer {
public:
class const_iterator {
friend class DomElementContainer;
public:
const_iterator(){}
const_iterator &operator++(){
m_e = m_e.nextSiblingElement(m_t);
return *this;
}
const QDomElement & operator*(){
return m_e;
}
bool operator==(const const_iterator &other) const {
return (m_e==other.m_e && m_t==other.m_t);
}
bool operator!=(const const_iterator &other) const {
return (m_e!=other.m_e || m_t!=other.m_t);
}
private:
const_iterator(const QDomElement &e, const QString &t){
m_e = e;
m_t = t;
}
QDomElement m_e;
QString m_t;
};
DomElementContainer(const QDomElement &e, const QString &tag=QString::null){
m_tag = tag;
m_elem = e;
}
DomElementContainer(const QDomDocument &d, const QString &tag=QString::null){
m_tag = tag;
m_elem = d.documentElement();
}
const_iterator begin() const{
return const_iterator(m_elem.firstChildElement(m_tag), m_tag);
}
const_iterator end() const {
return const_iterator(QDomElement(), m_tag);
}
private:
QDomElement m_elem;
QString m_tag;
};
class Qxml : public QDomDocument
{
public:
Qxml(); /* utf-8 - ISO-8859-1 */
QString decode;
inline QString Getdecode() { return decode; }
bool SetSelf( const QString fullFileName );
QDomElement insertFragmentorFile( QString fragment ); /* insert xml file or tag fragment */
QString StringtoXML( QString t ); /* xml_escape_chars */
void SetstreamFile( QString sfile ); /* set a work file */
bool saveXML(const QString fullFileName); /* save to external file */
bool saveXML(); /* save to work file if exist */
void Print(); /* print to console */
QString GetAtt(QDomElement e , QString name ); /* get attribute value from element */
QDomElement root();
QString FilterAttribute( QDomElement element , QString attribute );
QDomElement insertTagValue( const QString name , QString value );
protected:
bool is_file(QString fullFileName);
bool xml_unlink(QString fullFileName);
private:
QDomProcessingInstruction header;
QString stream_on_file;
QDomElement ErrorDom(); /* on error return </error> tag */
};
//
#endif // QXML_H
qxml.cpp
Code: Alles auswählen
#include "qxml.h"
/* save this file as qxml.cpp */
/* constructor */
Qxml::Qxml()
{
decode = "utf-8";
header = QDomDocument::createProcessingInstruction( "xml",QString("version=\"1.0\" encoding=\"%1\"" ).arg(decode));
appendChild( header );
}
QDomElement Qxml::root()
{
QDomElement root = documentElement();
return root;
}
bool Qxml::SetSelf( const QString fullFileName )
{
QString errorStr;
int errorLine, errorColumn, position;
QFile f( fullFileName );
if ( !f.exists(fullFileName) ) {
return false;
}
if(!f.open(QIODevice::ReadOnly)) {
return false;
}
/* no namen space ! */
if (!setContent(&f,false, &errorStr, &errorLine, &errorColumn)) {
return false;
} else {
return true;
}
}
/* save doc if is a streamfiele */
bool Qxml::saveXML()
{
if (is_file(stream_on_file)) {
return saveXML(stream_on_file);
}
return false;
}
/* print on cosole the result */
void Qxml::Print()
{
QTextStream out(stdout);
out << Qxml::toString();
}
QDomElement Qxml::insertTagValue( const QString name , QString value )
{
QDomElement yourtag = QDomDocument::createElement( name );
////QDomNode::setNodeValue( value ) ;
yourtag.appendChild(Qxml::createTextNode(value));
return yourtag;
}
/* Append fragment or a real xml file */
QDomElement Qxml::insertFragmentorFile( QString fragment )
{
bool check;
/* if is a file insert dom tree and convert to element */
if (is_file(fragment)) {
QFile ext_file(fragment);
ext_file.open( QIODevice::ReadOnly);
QDomDocument dom_external;
check = dom_external.setContent(&ext_file);
if ( check ) {
QDomElement root_extern = dom_external.documentElement();
QDomNode newnode = root_extern.firstChild();
QDomNode treallsub = dom_external.importNode(newnode,true);
ext_file.close();
return treallsub.toElement();
} else {
ext_file.close();
return ErrorDom();
}
} else {
/* is not a file */
QTextCodec *setcurrentcodec;
if (decode == "utf-8") {
setcurrentcodec = QTextCodec::codecForMib(106); /* utf8 */
} else {
setcurrentcodec = QTextCodec::codecForMib(4); /* latin iso */
}
if (stream_on_file.size() > 0) {
QFile f( stream_on_file );
if ( f.open( QFile::WriteOnly | QFile::Text ) ) {
QTextStream sw( &f );
sw.setCodec(setcurrentcodec);
sw << QString("<?xml version=\"1.0\"?>\n<dummyroot>%1</dummyroot>").arg(fragment);
f.close();
if (is_file(stream_on_file)) {
return insertFragmentorFile(stream_on_file);
}
}
}
return ErrorDom();
}
}
/* return on error an error tag */
QDomElement Qxml::ErrorDom()
{
QDomElement errorelement = QDomDocument::createElement( "error" );
return errorelement;
}
/* setup a file to stream fragments */
void Qxml::SetstreamFile( QString sfile )
{
stream_on_file = sfile;
}
QString Qxml::StringtoXML( QString t )
{
//the following checks are necessary before exporting
//strings to XML. see http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.html for details
QString text = t;
text.replace("&", "&"); /* qt4 toUtf8 dont replace && */
text.replace("\"",""");
text.replace("'", "'");
text.replace("<", "<");
text.replace(">", ">");
text.replace("\n", "
");
text.replace("\r", "
");
return text;
}
/* save to a specific file */
bool Qxml::saveXML( const QString fullFileName )
{
QTextCodec *setcurrentcodec;
if (decode == "utf-8") {
setcurrentcodec = QTextCodec::codecForMib(106); /* utf8 */
} else {
setcurrentcodec = QTextCodec::codecForMib(4); /* latin iso */
}
QFile f( fullFileName );
if ( f.open( QFile::WriteOnly | QFile::Text ) ) {
QTextStream sw( &f );
sw.setCodec(setcurrentcodec);
sw << Qxml::toString();
f.close();
if (is_file(fullFileName)) {
return true;
}
}
return false;
}
/* remove file */
QString Qxml::GetAtt(QDomElement e , QString name )
{
QString textvalue;
QString errorvalue = "";
QDomAttr a1 = e.attributeNode(name);
textvalue = a1.value();
if (textvalue.size() > 0) {
return textvalue;
} else {
return errorvalue;
}
}
/* loop to find attribute name xx */
QString Qxml::FilterAttribute( QDomElement element , QString attribute )
{
QString base = "error";
QDomNamedNodeMap attlist = element.attributes();
int bigint = attlist.count();
if ( bigint > 0 ) {
for (int i=0; i<bigint; i++){
QDomNode nod = attlist.item(i);
if (nod.nodeName() == attribute) {
base = QString(nod.nodeValue());
return base;
}
}
}
return base;
}
/* checker if is file exist */
bool Qxml::is_file(QString fullFileName)
{
QFile f( fullFileName );
if ( f.exists() ) {
return true;
} else {
return false;
}
}
/* remove file */
bool Qxml::xml_unlink(QString fullFileName)
{
QFile f( fullFileName );
if ( is_file( fullFileName ) ) {
if (f.remove()) {
return true;
}
}
return false;
}
.........................
speack português italiano deutsch english castellà qt