PDA

View Full Version : QDomDocument::setContent() returns true despite XML being invalid



Jits
6th September 2010, 09:12
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:


void Class::getXML()
{
domDoc->clear();
//Converts text edit content to HTML and parses to DOM doc
if(!domDoc->setContent(textEdit->toHtml())) {
QMessageBox::information(this,"Error","XML invalid");
} else {
QMessageBox::information(this,"Success",domDoc->toString(4)); //xxxTESTINGxxx
}
}

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

Thanks!

Lykurg
6th September 2010, 09:25
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.

hobbyist
6th September 2010, 10:33
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?

Jits
6th September 2010, 11:08
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?

Jits
6th September 2010, 11:09
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! :)

hobbyist
6th September 2010, 11:50
Perhaps you could give an example of your input?

As an example, I get


"tag mismatch" at line: 4 col: 10

when I run this:


#include <QtGui>
#include <QtXml>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QTextEdit textEdit;
QDomDocument domDoc;
QString error;
int line, col;

textEdit.append("<stuff>");
textEdit.append(" <testing>");
textEdit.append(" blaa... ");
textEdit.append(" </tstng>");
textEdit.append("</stuff>");

textEdit.show();

if(!domDoc.setContent(textEdit.toPlainText(), &error, &line, &col))
qDebug() << error << "at line: " << line << "col: " << col;

return app.exec();
}


[edit] Sorry, misunderstood the question, which was about schemas, not parsing.

Jits
6th September 2010, 13:38
[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!