If this is your first visit, be sure to
check out the
FAQ
by clicking the
link above. You may have to
register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
Welcome to
Qt Centre
.
Qt Centre
is a community site devoted to programming in C++ using the
Qt framework
. Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue —
register
and post your question.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please,
join our community today
!
If you have any problems with the registration process or your account login, please
contact us
.
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:
Switch view
void Class::getXML()
{
domDoc->clear();
//Converts text edit content to HTML and parses to DOM doc
if(!domDoc->setContent(textEdit->toHtml())) {
} else {
QMessageBox::information(this,
"Success",domDoc
->toString
(4));
//xxxTESTINGxxx }
}
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
To copy to clipboard, switch view to plain text mode
I understand setContent() is supposed to validate the input? Any ideas?
Thanks!
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?
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?
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!
"tag mismatch" at line: 4 col: 10
"tag mismatch" at line: 4 col: 10
To copy to clipboard, switch view to plain text mode
when I run this:
Qt Code:
Switch view
#include <QtGui>
#include <QtXml>
int main(int argc, char *argv[])
{
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();
}
#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();
To copy to clipboard, switch view to plain text mode
[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!