Results 1 to 10 of 10

Thread: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween elements

  1. #1
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Why Qxmlstreamreader returns pair of quotes and double empty lines beetween elements

    I have such xml-file contents written with Qxmlstreamwriter:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. - <FILESYSTEM>
    3. - <FILE>
    4. <NAME>autoc.JPG</NAME>
    5. <SIZE>19197</SIZE>
    6. <PATH>c:/c</PATH>
    7. </FILE>
    8. - <FILE>
    9. <NAME>copy autoc.JPG</NAME>
    10. <SIZE>19197</SIZE>
    11. <PATH>c:/c</PATH>
    12. </FILE>
    13. - <FILE>
    14. <NAME>copy copy autoc.JPG</NAME>
    15. <SIZE>19197</SIZE>
    16. <PATH>c:/c</PATH>
    17. </FILE>
    18. - <FILE>
    19. <NAME>copy copy copy autoc.JPG</NAME>
    20. <SIZE>19197</SIZE>
    21. <PATH>c:/c</PATH>
    22. </FILE>
    23. </FILESYSTEM>
    To copy to clipboard, switch view to plain text mode 
    I try to extract the same view and order of this xml to console (but then in GUI TextEdit) with such
    simple function:
    Qt Code:
    1. void Mainclas::readxmlfile()
    2. {
    3. QXmlStreamReader Rxml;
    4. QString filename="D:\\file00.xml";
    5. QFile file(filename);
    6. file.open(QIODevice::ReadOnly);
    7. Rxml.setDevice(&file);
    8. Rxml.readNext();
    9. while(!Rxml.atEnd())
    10. {
    11. if(Rxml.isStartDocument()) {
    12. qDebug()<<"<?xml"<<Rxml.documentEncoding ().toString()<<" "<<Rxml.documentVersion().toString()<<"?>";
    13. Rxml.readNext();
    14. }
    15. if(Rxml.isStartElement()) {
    16. qDebug()<<"<"+Rxml.name().toString()<<">";
    17. Rxml.readNext();
    18. }
    19. if(Rxml.isEndElement()) {
    20. qDebug()<<"</"+Rxml.name().toString()<<">";
    21. Rxml.readNext();
    22. }
    23. if(Rxml.isCharacters ()) {
    24. if(Rxml.text()=="") Rxml.readNext();
    25. qDebug()<<Rxml.text();
    26. Rxml.readNext();
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    But I got
    Qt Code:
    1. "<FILESYSTEM" >
    2. "
    3. "
    4. <FILE>
    To copy to clipboard, switch view to plain text mode 
    Here is console printscreen - readxml.jpg
    Why it happened? how to resolve it?
    Should it be some error due to absent of such eroor-handler?
    Last edited by artt; 8th December 2015 at 22:08.

  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: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    You are probably missing a readNext() call.
    Better put it at the beginning or the end of the loop.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    There are several readNext() 's- where I should put next one?
    Here is similar question but also without answer -
    http://www.qtcentre.org/threads/6454...ments?p=285154

  4. #4
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    What would you do better in this situation (to extract the xml identically), if have not any suggestion concerning quotes?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    The example you post is not well formed XML. I would be surprised if it was written by QXmlStreamWriter.

    In all likelihood, the output at lines 2 and 3 is put there by line 25 of your code. QDebug added the quotes and between the quotes are the end of line character and leading spaces from two consecutive lines in the input file. QDebug is a debugging tool not a general purpose output generating tool. The quotes are useful to help tell the difference between an empty string "" and one containing only whitespace characters " ".

    If you want a general purpose text output stream then look at QTextStream.

  6. #6
    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: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    Quote Originally Posted by artt View Post
    There are several readNext() 's- where I should put next one?
    The readNext() statements are all in weird places.
    There should be only one at either the beginning or end that advances the stream.

    Cheers,
    _

  7. #7
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    Really, in case of just one readnext() - in the beginning or the end of while loop we can have xml display -but the same in every detail - so my approach is correct and it doesnt influence the quotes - can I directly ask this question in trolltech team?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    You have already been told where the quotes came from and it has nothing to do with the QXmlStreamReader.

    Trolltech has not existed since circa 2008when it became part of Nokia and later the Qt Company

  9. #9
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    Quote Originally Posted by ChrisW67 View Post
    The example you post is not well formed XML. I would be surprised if it was written by QXmlStreamWriter.

    In all likelihood, the output at lines 2 and 3 is put there by line 25 of your code. QDebug added the quotes and between the quotes are the end of line character and leading spaces from two consecutive lines in the input file. QDebug is a debugging tool not a general purpose output generating tool. The quotes are useful to help tell the difference between an empty string "" and one containing only whitespace characters " ".

    If you want a general purpose text output stream then look at QTextStream.
    No it seems it is well formed XML, and it is strictly craeted by QXmlStreamWriter.
    Tes, the reason is in QDebug.
    When I used such if {} block:
    Qt Code:
    1. if(Rxml.isStartElement()) {
    2. QTextStream cout(stdout,QIODevice::WriteOnly);
    3. cout<<"<"+Rxml.name().toString()<<">";
    4. //qDebug()<<'<'+Rxml.name().toAscii()<<'>';
    5. //Rxml.readNext();
    6. }
    To copy to clipboard, switch view to plain text mode 
    I got usual XML except NAME tag:
    Qt Code:
    1. <?xmlUTF-8 1.0?>
    2. <FILESYSTEM>
    3. <FILE>
    4. <NAME>autoc.JPG</NAME>
    5. <SIZE>19197</SIZE>
    6. <PATH>c:/c</PATH>
    7. </FILE>
    8. <FILE>
    9. <NAME>copy autoc.JPG</NAME>
    10. <SIZE>19197</SIZE>
    11. <PATH>c:/c</PATH>
    12. </FILE>
    13. <FILE>
    14. <NAME>copy copy autoc.JPG</NAME>
    15. <SIZE>19197</SIZE>
    16. <PATH>c:/c</PATH>
    17. </FILE>
    18. <FILE>
    19. <NAME>copy copy copy autoc.JPG</NAME>
    20. <SIZE>19197</SIZE>
    21. <PATH>c:/c</PATH>
    22. </FILE>
    23. </FILESYSTEM>
    To copy to clipboard, switch view to plain text mode 
    Should it be due the fact that Name is the first sub-element(text element) of FILE?
    How to do it normal without that omission as
    NAME, SIZE and PATH are identical...


    Added after 6 minutes:


    This situation is probably to this one, if you see:
    Qt Code:
    1. "<FILESYSTEM" >
    2. "
    3. "
    4. <FILE>
    5. "
    6. "
    7. <NAME>
    To copy to clipboard, switch view to plain text mode 
    as during writing to qxmlstreamwriter I put such character as "\r\n" (that is break-line) between FILE start element and NAME start element
    Last edited by artt; 11th December 2015 at 00:21.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why Qxmlstreamreader returns pair of quotes and double empty lines beetween eleme

    No it seems it is well formed XML, and it is strictly craeted by QXmlStreamWriter.
    From your first post:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. - <FILESYSTEM>
    3. - <FILE>
    4. <NAME>autoc.JPG</NAME>
    5. <SIZE>19197</SIZE>
    6. <PATH>c:/c</PATH>
    7. </FILE>
    8. - <FILE>
    9. <NAME>copy autoc.JPG</NAME>
    10. <SIZE>19197</SIZE>
    11. <PATH>c:/c</PATH>
    12. </FILE>
    13. - <FILE>
    14. <NAME>copy copy autoc.JPG</NAME>
    15. <SIZE>19197</SIZE>
    16. <PATH>c:/c</PATH>
    17. </FILE>
    18. - <FILE>
    19. <NAME>copy copy copy autoc.JPG</NAME>
    20. <SIZE>19197</SIZE>
    21. <PATH>c:/c</PATH>
    22. </FILE>
    23. </FILESYSTEM>
    To copy to clipboard, switch view to plain text mode 
    The hyphen '-' on line 2 is certainly not well formed XML: "Line 2 Column1 Content is not allowed in prolog". The other hyphens may or may not be correct.

    Feed that to this simple reader:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QFile>
    3. #include <QXmlStreamReader>
    4. #include <QDebug>
    5.  
    6. void readXmlFile() {
    7. QFile input("test.xml");
    8. if (input.open(QIODevice::ReadOnly)) {
    9. // This block structure straight from the Qt docs
    10. QXmlStreamReader xml(&input);
    11. while (!xml.atEnd()) {
    12. xml.readNext();
    13. // ... do processing
    14. if (xml.isStartDocument()) {
    15. qDebug() << "Start document";
    16. }
    17. else if (xml.isEndDocument()) {
    18. qDebug() << "End document";
    19. }
    20. else if (xml.isStartElement()) {
    21. qDebug() << "Start element:" << xml.name();
    22. }
    23. else if (xml.isEndElement()) {
    24. qDebug() << "End element:" << xml.name();
    25. }
    26. else if (xml.isCharacters()) {
    27. qDebug() << "Characters:" << xml.text();
    28. }
    29. else {
    30. qDebug() << "Ignored something else";
    31. }
    32. }
    33. if (xml.hasError()) {
    34. // ... do error handling
    35. qDebug() << "Error:" << xml.error() << xml.errorString();
    36. }
    37. }
    38. else {
    39. qDebug() << "Failed to open file";
    40. }
    41. }
    42.  
    43. int main(int argc, char **argv) {
    44. QCoreApplication app(argc, argv);
    45. readXmlFile();
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 
    and you get
    Qt Code:
    1. Start document
    2. Ignored something else
    3. Error: 3 "Start tag expected."
    To copy to clipboard, switch view to plain text mode 

    I am struggling to see what you are trying to achieve. If you just want to reproduce the XML file then just copy it.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Replies: 7
    Last Post: 21st March 2014, 10:24
  2. Replies: 2
    Last Post: 5th December 2013, 06:35
  3. Replies: 1
    Last Post: 30th November 2013, 11:03
  4. Replies: 9
    Last Post: 22nd February 2013, 12:27
  5. Replies: 3
    Last Post: 27th December 2009, 00:00

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.