PDA

View Full Version : QXmlStreamWriter import node childs?



patrik08
5th September 2008, 14:56
How i can port this recursive node on a QXmlStreamWriter?
Is this possibel?

I see QXmlStreamWriter direct on QIODevice is faster and i like to use ...

I need only to write svg xml vector image, and the QStandardItemModel pdf bookmark




QDomElement FopDom::bookMarkTree( const QByteArray xmlt )
{
StreamFop *buf = new StreamFop(); /* Ram QIODevice QDomDocument creator */
buf->device()->write( xmlt );
if (buf->isValid()) {
QDomDocument bbdoc = buf->Dom(); /* new doc from external chunk */
QDomElement bbroot = bbdoc.documentElement();
QDomElement e = bbroot.firstChildElement("fo:bookmark-tree");
QDomElement Pbb = dom.createElement("fo:bookmark-tree");
QDomNamedNodeMap alist = e.attributes();
for (int i=0; i<alist.count(); i++){
QDomNode nod = alist.item(i);
Pbb.setAttribute(nod.nodeName().toLower(),nod.node Value());
}
QDomNode child = e.firstChild();
while ( !child.isNull() ) {
if ( child.isElement() ) {
Pbb.appendChild(dom.importNode(child,true).toEleme nt());
}
child = child.nextSibling();
}
return Pbb;
} else {
return dom.createElement("dummy");
}
}

patrik08
6th September 2008, 08:43
I try writeCurrentToken but it not make is job why?




#include <QtGui>
/////#include "qzipreader_p.h"
//////#include "qzipwriter_p.h"
#include <QFileInfo>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>

class DocumentWriter
{
public:

typedef enum
{
DOC_NOT_KNOW = 404,
DOC_XHTML = 100,
DOC_ODT = 200,
DOC_FOP = 300,
DOC_RTF = 400,
DOC_XML = 500,
DOC_PAGE = 600 /* binary */
} WDOCTYPE;

DocumentWriter( const QString &fileName )
: de(0),cod(QTextCodec::codecForHtml("utf-8")),type(DOC_FOP)
{
QFileInfo fi(fileName);
const QString ext = fi.completeSuffix().toLower();
if (ext == "xml") {
type = DOC_XML;
} else if (ext == "html") {
type = DOC_XHTML;
} else if (ext == "odt") {
type = DOC_ODT;
}

QFile *file = new QFile(fileName);
if (file->open(QIODevice::WriteOnly)) {
qDebug() << "### file open ---------- " << fileName;
de = file;
}

ft = cod->name();
}
~DocumentWriter() {
de->close();
}

QIODevice *device() { return de; }
QByteArray format() { return ft; }
QTextCodec *codec() { return cod; }
QByteArray ft;
QIODevice *de;
QTextCodec *cod;
WDOCTYPE type;
};


int main( int argc, char ** argv )
{
QApplication a( argc, argv );
QString txtlog;

DocumentWriter one("dummytest.xml");
QString fopNS (QLatin1String("http://www.w3.org/1999/XSL/Format"));
QString svgNS (QLatin1String("http://www.w3.org/2000/svg"));
QString xlinkNS (QLatin1String("http://www.w3.org/1999/xlink"));
QString cmsNS (QLatin1String("http://www.pulitzer.ch/2007/CMSFormat"));

QXmlStreamWriter writer(one.device());
writer.setCodec(QTextCodec::codecForName("utf-8"));
writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(2);

qDebug() << "### xml on codec init ---------- " << writer.codec()->name();

writer.writeNamespace(fopNS, QString::fromLatin1("fo"));
writer.writeNamespace(svgNS, QString::fromLatin1("svg"));
writer.writeNamespace(xlinkNS, QString::fromLatin1("uri"));
writer.writeNamespace(cmsNS, QString::fromLatin1("cms"));

writer.writeStartDocument();
writer.writeStartElement(fopNS, QString::fromLatin1("root"));

writer.writeStartElement(fopNS, QString::fromLatin1("flow"));

for (int i = 0; i < 10000; ++i) {
writer.writeStartElement(fopNS, QString::fromLatin1("block"));
writer.writeAttribute(fopNS, QString::fromLatin1("padding-top"),QString("10"));
writer.writeCharacters (QString("Line nummer %1").arg(i) );
writer.writeStartElement(fopNS, QString::fromLatin1("inline"));
/* append sub node to last elements */
QXmlStreamReader chidxml(QString("<root><node>data</node></root>"));
////////while (!chidxml.atEnd()) {
if (!chidxml.hasError()) {
writer.writeCurrentToken(chidxml);
}
///////}

writer.writeEndElement();
writer.writeEndElement();
}
///////writer.writeEndElement();
///////writer.writeEndElement();
////////writer.writeEndElement();
///////////writer.writeEndElement();
qDebug() << "### xml on codec end ---------- " << writer.codec()->name();
writer.writeEndDocument();
QTextEdit t;
t.setPlainText ( txtlog );
t.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}

//////#include "main.moc"

patrik08
6th September 2008, 12:27
I solved now ChildImport::copyDeep work similar to dom import child it read the entire tree or it is possible to append a flag from which node name...






#include <QtGui>
/////#include "qzipreader_p.h"
//////#include "qzipwriter_p.h"
#include <QFileInfo>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>

class ChildImport : public QXmlStreamReader
{
public:
ChildImport( QIODevice* device = 0 )
:QXmlStreamReader( device )
{
setNamespaceProcessing(false);
/* only prefix append if exist */
}
/* copy all subelement from reader to writer out */
void copyDeep( QIODevice* device , QXmlStreamWriter &out )
{
setDevice(device);
while (!atEnd()) {
readNext();
if ( isStartElement() ) {
/* a dom element node is start to read */
/////qDebug() << "### copydeep element tagname ---------- " << name().toString();
/////////qDebug() << "### copydeep element tagname ---------- " << prefix().toString();
if (prefix().toString().isEmpty()) {
out.writeStartElement(name().toString());
} else {
out.writeStartElement(prefix().toString()+":"+name().toString());
}
if (attributes().size() > 0) {
out.writeAttributes(attributes());
}
} else if (!isWhitespace()) {
/* element having text or ??? */
out.writeCharacters(text().toString());
} else if (isComment()) {
/* leave not import */
} else if (isCDATA()) {
/* take cdata */
out.writeComment(text().toString());
}


}
const int erno = (int)error();
if (erno != 0) {
qWarning() << "### ChildImport::copyDeep error ---------- " << error();
} else {
out.writeEndElement();
}
device->close();
}

};


class ReadWriteBuf
{
public:
ReadWriteBuf( const QString xml = QString() )
:d(new QBuffer())
{
d->open(QIODevice::ReadWrite);
start();
if (xml.size() > 0 )
write(xml);
}
bool clear()
{
d->write(QByteArray());
start();
return d->bytesAvailable() == 0 ? true : false;
}
qint64 write( const QString dat )
{
QByteArray chunk;
chunk.append(dat);
d->write(chunk);
start();
return d->bytesAvailable();
}
qint64 write( const QByteArray dat )
{
d->write(dat);
start();
return d->bytesAvailable();
}
void start() {
d->seek(0);
}
bool LoadFile( const QString file ) {
if (clear()) {
QFile f(file);
if (f.exists()) {
if (f.open(QFile::ReadOnly)) {
d->write(f.readAll());
f.close();
start();
return true;
}
}
}
return false;
}
bool PutOnFile( const QString file ) {
QFile f(file);
start();
if (f.open(QFile::WriteOnly)) {
uint bi = f.write(d->readAll());
f.close();
start();
return bi > 0 ? true : false;
}
return false;
}

QIODevice *device() { return d; }
QByteArray stream() { return d->data(); }
QString toString() { return QString(d->data()); }
QBuffer *d;
};



int main( int argc, char ** argv )
{
QApplication a( argc, argv );
QString txtlog;
ChildImport job(0);

QString fopNS (QLatin1String("http://www.w3.org/1999/XSL/Format"));
QString svgNS (QLatin1String("http://www.w3.org/2000/svg"));
////QString xlinkNS (QLatin1String("http://www.w3.org/1999/xlink"));
//////QString cmsNS (QLatin1String("http://www.pulitzer.ch/2007/CMSFormat"));

/* start a new xml to write */
ReadWriteBuf *Awriter = new ReadWriteBuf();
QXmlStreamWriter writer0(Awriter->device());


writer0.setCodec(QTextCodec::codecForName("utf-8"));
writer0.setAutoFormatting(true);
writer0.setAutoFormattingIndent(2);
writer0.writeNamespace(fopNS, QString::fromLatin1("fo"));
writer0.writeStartElement(fopNS, QString::fromLatin1("initroot"));

for (int i = 0; i < 400; ++i) {
writer0.writeStartElement(fopNS, QString::fromLatin1("frame"));
writer0.writeAttribute("id",QString("pos_%1").arg(i));
/* create a temp buffer to import */

ReadWriteBuf *appendTree = new ReadWriteBuf(
QString("<inline xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" "
"margin-start=\"1cm\"><fo:chars lang=\"en\">data</fo:chars></inline>") );
job.copyDeep(appendTree->device(),writer0);
writer0.writeEndElement();
writer0.writeEndElement();
}
writer0.writeEndDocument();
txtlog.append(QString(Awriter->stream()));
QTextEdit t;
t.setPlainText ( txtlog );
t.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}

//////#include "main.moc"