Results 1 to 7 of 7

Thread: QDomDocument::setContent() returns true despite XML being invalid

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QDomDocument::setContent() returns true despite XML being invalid

    Good day!

    Attempting to parse XML to DOM from string input using QDomDocument::setContent(). However, setContent() returns true regardless of the input and it's driving me nuts.

    Text is entered into QTextEdit GUI and then (theoretically) converted to XML in the following function:

    Qt Code:
    1. void Class::getXML()
    2. {
    3. domDoc->clear();
    4. //Converts text edit content to HTML and parses to DOM doc
    5. if(!domDoc->setContent(textEdit->toHtml())) {
    6. QMessageBox::information(this,"Error","XML invalid");
    7. } else {
    8. QMessageBox::information(this,"Success",domDoc->toString(4)); //xxxTESTINGxxx
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    I understand setContent() is supposed to validate the input? Any ideas?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    I am not sure, if only a CDATA like in you case is valid XML... Nevertheless I guess you expect a certain XML string, so have a look at XML Schema Validation Example in the docs, which allows you a more specific test.

  3. #3
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    Afaik, QTextEdit::toHtml() always returns valid html (see qDebug() << textEdit->toHtml() for the actual content).

    Perhaps domDoc->setContent(textEdit->toPlainText()) is what you are looking for?

  4. #4
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    Quote Originally Posted by Lykurg View Post
    I am not sure, if only a CDATA like in you case is valid XML... Nevertheless I guess you expect a certain XML string, so have a look at XML Schema Validation Example in the docs, which allows you a more specific test.
    Thanks for the reply! My XML knowledge is rudimentary at best, but I believe there are many different schemas we'll be using (this little app is for in-house testing) so I'm starting to wonder if I should just forget about the validation and hope that the users will check their XML syntax as I'm really struggling to make this work.

    What I don't understand is that the parsing to DOM doesn't return any errors...ever.Shouldn't there be complaints if there are, e.g. missing brackets?

  5. #5
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    Quote Originally Posted by hobbyist View Post
    Afaik, QTextEdit::toHtml() always returns valid html (see qDebug() << textEdit->toHtml() for the actual content).

    Perhaps domDoc->setContent(textEdit->toPlainText()) is what you are looking for?
    Doesn't seem to do any validation either...brackets or no brackets, it's happy!

  6. #6
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    Perhaps you could give an example of your input?

    As an example, I get
    Qt Code:
    1. "tag mismatch" at line: 4 col: 10
    To copy to clipboard, switch view to plain text mode 
    when I run this:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtXml>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QTextEdit textEdit;
    9. QDomDocument domDoc;
    10. QString error;
    11. int line, col;
    12.  
    13. textEdit.append("<stuff>");
    14. textEdit.append(" <testing>");
    15. textEdit.append(" blaa... ");
    16. textEdit.append(" </tstng>");
    17. textEdit.append("</stuff>");
    18.  
    19. textEdit.show();
    20.  
    21. if(!domDoc.setContent(textEdit.toPlainText(), &error, &line, &col))
    22. qDebug() << error << "at line: " << line << "col: " << col;
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    [edit] Sorry, misunderstood the question, which was about schemas, not parsing.
    Last edited by hobbyist; 6th September 2010 at 12:35.

  7. #7
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument::setContent() returns true despite XML being invalid

    Quote Originally Posted by hobbyist View Post
    [edit] Sorry, misunderstood the question, which was about schemas, not parsing.
    You won't believe me...but your code helped me figure this out. Seeing that yours produced the correct results, I finally determined that my TEST function was wrong! All this time wasted because I never considered that angle! Feels like such an idiot...

    Thank you both for the input!

Similar Threads

  1. Can't see widget, yet isVisible() returns true
    By MattPhillips in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2010, 12:56
  2. Replies: 2
    Last Post: 2nd October 2009, 15:32
  3. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 09:59
  4. Strange error: doc.setContent(data) returns false
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2009, 01:38
  5. connect() returns true but slot not called
    By OriginalCopy in forum Qt Programming
    Replies: 6
    Last Post: 4th November 2007, 18:54

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.