Results 1 to 5 of 5

Thread: writing raw data into xml sub-node

  1. #1
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default writing raw data into xml sub-node

    (continuing http://www.qtcentre.org/threads/6000...-and-sub-nodes )

    I am using QXmlStreamWriter, and under some conditions I need to write into a node, a text that contains raw xml, not encoded.

    Qt Code:
    1. QString b = "<b>test 1 2 3 </b>";
    2. QXmlStreamWriter stream(&output);
    3. stream.writeStartDocument();
    4. stream.writeStartElement("a");
    5. stream.writeCharacters(b);
    6. stream.writeEndElement();
    7. stream.writeEndDocument();
    8. stream.close();
    To copy to clipboard, switch view to plain text mode 

    In this example, "b" gets encoded into XML, which is usually desired, but not in this case.

    One solution I have is to write the XML manually as a text file. This will work, but I feel this is ugly.

    What other alternatives to I have?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: writing raw data into xml sub-node

    Have you tried writing into "output" after calling writeStartElement("a")?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: writing raw data into xml sub-node

    Is this what you recommend?

    Qt Code:
    1. void test_save() {
    2. QString b = "<b>test 1 2 3 </b>";
    3. QByteArray bytes;
    4. QBuffer output(&bytes);
    5. output.open(QIODevice::ReadWrite);
    6.  
    7. QXmlStreamWriter stream(&output);
    8. stream.writeStartDocument();
    9. stream.writeStartElement("a");
    10. output.write(b.toLatin1().constData(), b.length());
    11. stream.writeEndElement();
    12. stream.writeEndDocument();
    13. output.close();
    14. qDebug("%s", bytes.data());
    15. }
    To copy to clipboard, switch view to plain text mode 

    This outputs:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?><a<b>test 1 2 3 </b>/>
    To copy to clipboard, switch view to plain text mode 

    See how "a" element is not closed? Note also how the "</a>" is missing, as well as document end. I wonder why it broke off so much.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: writing raw data into xml sub-node

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing raw data into xml sub-node

    See how "a" element is not closed? Note also how the "</a>" is missing, as well as document end. I wonder why it broke off so much.
    Because you told QXmlStreamWriter to write an empty "<a/>" element. By calling output.write() directly, you did something that the stream writer has no knowledge about. It doesn't know what you did "behind its back". As far as it knows, all you did was to tell it to start an "a" element, then immediately end it.

    Try wysota's idea.

    But why can't you simply write out the raw XML using QXmlStreamWriter? Are the "<b>" element names unknown at compile time? Or is it only the text inside the "b" element?

Similar Threads

  1. writing random data into image
    By av2306 in forum Qt Programming
    Replies: 4
    Last Post: 18th November 2013, 12:36
  2. QTcpSocket Writing Data Progress
    By lwinhtooko in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 21:10
  3. how to justfy a node is leaf node or not
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2009, 08:40
  4. Writing Data Aware Forms in QT4.
    By gsQT4 in forum Qt Programming
    Replies: 5
    Last Post: 21st March 2007, 11:35
  5. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 21:51

Tags for this Thread

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.