Results 1 to 3 of 3

Thread: QXmlStreamWriter import node childs?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QXmlStreamWriter import node childs?

    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

    Qt Code:
    1. QDomElement FopDom::bookMarkTree( const QByteArray xmlt )
    2. {
    3. StreamFop *buf = new StreamFop(); /* Ram QIODevice QDomDocument creator */
    4. buf->device()->write( xmlt );
    5. if (buf->isValid()) {
    6. QDomDocument bbdoc = buf->Dom(); /* new doc from external chunk */
    7. QDomElement bbroot = bbdoc.documentElement();
    8. QDomElement e = bbroot.firstChildElement("fo:bookmark-tree");
    9. QDomElement Pbb = dom.createElement("fo:bookmark-tree");
    10. QDomNamedNodeMap alist = e.attributes();
    11. for (int i=0; i<alist.count(); i++){
    12. QDomNode nod = alist.item(i);
    13. Pbb.setAttribute(nod.nodeName().toLower(),nod.nodeValue());
    14. }
    15. QDomNode child = e.firstChild();
    16. while ( !child.isNull() ) {
    17. if ( child.isElement() ) {
    18. Pbb.appendChild(dom.importNode(child,true).toElement());
    19. }
    20. child = child.nextSibling();
    21. }
    22. return Pbb;
    23. } else {
    24. return dom.createElement("dummy");
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamWriter import node childs?

    I try writeCurrentToken but it not make is job why?


    Qt Code:
    1. #include <QtGui>
    2. /////#include "qzipreader_p.h"
    3. //////#include "qzipwriter_p.h"
    4. #include <QFileInfo>
    5. #include <QXmlStreamWriter>
    6. #include <QXmlStreamReader>
    7.  
    8. class DocumentWriter
    9. {
    10. public:
    11.  
    12. typedef enum
    13. {
    14. DOC_NOT_KNOW = 404,
    15. DOC_XHTML = 100,
    16. DOC_ODT = 200,
    17. DOC_FOP = 300,
    18. DOC_RTF = 400,
    19. DOC_XML = 500,
    20. DOC_PAGE = 600 /* binary */
    21. } WDOCTYPE;
    22.  
    23. DocumentWriter( const QString &fileName )
    24. : de(0),cod(QTextCodec::codecForHtml("utf-8")),type(DOC_FOP)
    25. {
    26. QFileInfo fi(fileName);
    27. const QString ext = fi.completeSuffix().toLower();
    28. if (ext == "xml") {
    29. type = DOC_XML;
    30. } else if (ext == "html") {
    31. type = DOC_XHTML;
    32. } else if (ext == "odt") {
    33. type = DOC_ODT;
    34. }
    35.  
    36. QFile *file = new QFile(fileName);
    37. if (file->open(QIODevice::WriteOnly)) {
    38. qDebug() << "### file open ---------- " << fileName;
    39. de = file;
    40. }
    41.  
    42. ft = cod->name();
    43. }
    44. ~DocumentWriter() {
    45. de->close();
    46. }
    47.  
    48. QIODevice *device() { return de; }
    49. QByteArray format() { return ft; }
    50. QTextCodec *codec() { return cod; }
    51. QIODevice *de;
    52. QTextCodec *cod;
    53. WDOCTYPE type;
    54. };
    55.  
    56.  
    57. int main( int argc, char ** argv )
    58. {
    59. QApplication a( argc, argv );
    60. QString txtlog;
    61.  
    62. DocumentWriter one("dummytest.xml");
    63. QString fopNS (QLatin1String("http://www.w3.org/1999/XSL/Format"));
    64. QString svgNS (QLatin1String("http://www.w3.org/2000/svg"));
    65. QString xlinkNS (QLatin1String("http://www.w3.org/1999/xlink"));
    66. QString cmsNS (QLatin1String("http://www.pulitzer.ch/2007/CMSFormat"));
    67.  
    68. QXmlStreamWriter writer(one.device());
    69. writer.setCodec(QTextCodec::codecForName("utf-8"));
    70. writer.setAutoFormatting(true);
    71. writer.setAutoFormattingIndent(2);
    72.  
    73. qDebug() << "### xml on codec init ---------- " << writer.codec()->name();
    74.  
    75. writer.writeNamespace(fopNS, QString::fromLatin1("fo"));
    76. writer.writeNamespace(svgNS, QString::fromLatin1("svg"));
    77. writer.writeNamespace(xlinkNS, QString::fromLatin1("uri"));
    78. writer.writeNamespace(cmsNS, QString::fromLatin1("cms"));
    79.  
    80. writer.writeStartDocument();
    81. writer.writeStartElement(fopNS, QString::fromLatin1("root"));
    82.  
    83. writer.writeStartElement(fopNS, QString::fromLatin1("flow"));
    84.  
    85. for (int i = 0; i < 10000; ++i) {
    86. writer.writeStartElement(fopNS, QString::fromLatin1("block"));
    87. writer.writeAttribute(fopNS, QString::fromLatin1("padding-top"),QString("10"));
    88. writer.writeCharacters (QString("Line nummer %1").arg(i) );
    89. writer.writeStartElement(fopNS, QString::fromLatin1("inline"));
    90. /* append sub node to last elements */
    91. QXmlStreamReader chidxml(QString("<root><node>data</node></root>"));
    92. ////////while (!chidxml.atEnd()) {
    93. if (!chidxml.hasError()) {
    94. writer.writeCurrentToken(chidxml);
    95. }
    96. ///////}
    97.  
    98. writer.writeEndElement();
    99. writer.writeEndElement();
    100. }
    101. ///////writer.writeEndElement();
    102. ///////writer.writeEndElement();
    103. ////////writer.writeEndElement();
    104. ///////////writer.writeEndElement();
    105. qDebug() << "### xml on codec end ---------- " << writer.codec()->name();
    106. writer.writeEndDocument();
    107. t.setPlainText ( txtlog );
    108. t.show();
    109. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    110. return a.exec();
    111. }
    112.  
    113. //////#include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamWriter import node childs?

    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...




    Qt Code:
    1. #include <QtGui>
    2. /////#include "qzipreader_p.h"
    3. //////#include "qzipwriter_p.h"
    4. #include <QFileInfo>
    5. #include <QXmlStreamWriter>
    6. #include <QXmlStreamReader>
    7.  
    8. class ChildImport : public QXmlStreamReader
    9. {
    10. public:
    11. ChildImport( QIODevice* device = 0 )
    12. :QXmlStreamReader( device )
    13. {
    14. setNamespaceProcessing(false);
    15. /* only prefix append if exist */
    16. }
    17. /* copy all subelement from reader to writer out */
    18. void copyDeep( QIODevice* device , QXmlStreamWriter &out )
    19. {
    20. setDevice(device);
    21. while (!atEnd()) {
    22. readNext();
    23. if ( isStartElement() ) {
    24. /* a dom element node is start to read */
    25. /////qDebug() << "### copydeep element tagname ---------- " << name().toString();
    26. /////////qDebug() << "### copydeep element tagname ---------- " << prefix().toString();
    27. if (prefix().toString().isEmpty()) {
    28. out.writeStartElement(name().toString());
    29. } else {
    30. out.writeStartElement(prefix().toString()+":"+name().toString());
    31. }
    32. if (attributes().size() > 0) {
    33. out.writeAttributes(attributes());
    34. }
    35. } else if (!isWhitespace()) {
    36. /* element having text or ??? */
    37. out.writeCharacters(text().toString());
    38. } else if (isComment()) {
    39. /* leave not import */
    40. } else if (isCDATA()) {
    41. /* take cdata */
    42. out.writeComment(text().toString());
    43. }
    44.  
    45.  
    46. }
    47. const int erno = (int)error();
    48. if (erno != 0) {
    49. qWarning() << "### ChildImport::copyDeep error ---------- " << error();
    50. } else {
    51. out.writeEndElement();
    52. }
    53. device->close();
    54. }
    55.  
    56. };
    57.  
    58.  
    59. class ReadWriteBuf
    60. {
    61. public:
    62. ReadWriteBuf( const QString xml = QString() )
    63. :d(new QBuffer())
    64. {
    65. d->open(QIODevice::ReadWrite);
    66. start();
    67. if (xml.size() > 0 )
    68. write(xml);
    69. }
    70. bool clear()
    71. {
    72. d->write(QByteArray());
    73. start();
    74. return d->bytesAvailable() == 0 ? true : false;
    75. }
    76. qint64 write( const QString dat )
    77. {
    78. QByteArray chunk;
    79. chunk.append(dat);
    80. d->write(chunk);
    81. start();
    82. return d->bytesAvailable();
    83. }
    84. qint64 write( const QByteArray dat )
    85. {
    86. d->write(dat);
    87. start();
    88. return d->bytesAvailable();
    89. }
    90. void start() {
    91. d->seek(0);
    92. }
    93. bool LoadFile( const QString file ) {
    94. if (clear()) {
    95. QFile f(file);
    96. if (f.exists()) {
    97. if (f.open(QFile::ReadOnly)) {
    98. d->write(f.readAll());
    99. f.close();
    100. start();
    101. return true;
    102. }
    103. }
    104. }
    105. return false;
    106. }
    107. bool PutOnFile( const QString file ) {
    108. QFile f(file);
    109. start();
    110. if (f.open(QFile::WriteOnly)) {
    111. uint bi = f.write(d->readAll());
    112. f.close();
    113. start();
    114. return bi > 0 ? true : false;
    115. }
    116. return false;
    117. }
    118.  
    119. QIODevice *device() { return d; }
    120. QByteArray stream() { return d->data(); }
    121. QString toString() { return QString(d->data()); }
    122. QBuffer *d;
    123. };
    124.  
    125.  
    126.  
    127. int main( int argc, char ** argv )
    128. {
    129. QApplication a( argc, argv );
    130. QString txtlog;
    131. ChildImport job(0);
    132.  
    133. QString fopNS (QLatin1String("http://www.w3.org/1999/XSL/Format"));
    134. QString svgNS (QLatin1String("http://www.w3.org/2000/svg"));
    135. ////QString xlinkNS (QLatin1String("http://www.w3.org/1999/xlink"));
    136. //////QString cmsNS (QLatin1String("http://www.pulitzer.ch/2007/CMSFormat"));
    137.  
    138. /* start a new xml to write */
    139. ReadWriteBuf *Awriter = new ReadWriteBuf();
    140. QXmlStreamWriter writer0(Awriter->device());
    141.  
    142.  
    143. writer0.setCodec(QTextCodec::codecForName("utf-8"));
    144. writer0.setAutoFormatting(true);
    145. writer0.setAutoFormattingIndent(2);
    146. writer0.writeNamespace(fopNS, QString::fromLatin1("fo"));
    147. writer0.writeStartElement(fopNS, QString::fromLatin1("initroot"));
    148.  
    149. for (int i = 0; i < 400; ++i) {
    150. writer0.writeStartElement(fopNS, QString::fromLatin1("frame"));
    151. writer0.writeAttribute("id",QString("pos_%1").arg(i));
    152. /* create a temp buffer to import */
    153.  
    154. ReadWriteBuf *appendTree = new ReadWriteBuf(
    155. QString("<inline xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" "
    156. "margin-start=\"1cm\"><fo:chars lang=\"en\">data</fo:chars></inline>") );
    157. job.copyDeep(appendTree->device(),writer0);
    158. writer0.writeEndElement();
    159. writer0.writeEndElement();
    160. }
    161. writer0.writeEndDocument();
    162. txtlog.append(QString(Awriter->stream()));
    163. t.setPlainText ( txtlog );
    164. t.show();
    165. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    166. return a.exec();
    167. }
    168.  
    169. //////#include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.