添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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
  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. }
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!
  1. "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
  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. }
#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!