Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDomDocument fails to set content of an HTML document with <!doctype> tag

When I use QDomDocument with HTML content, it fails to set content if there is a <!doctype html> at the beginning of the document. But actually why?! for example consider the following snippet of code:

 QDomDocument doc;
 QString content = "<!doctype html><html><body><a href='bar'>foo</a></body></html>";
 qDebug() << doc.setContent(content,false,0,0);
 QDomElement docElem = doc.documentElement();
 QDomNode a = docElem.firstChild();
 qDebug() << doc.childNodes().size() << docElem.childNodes().size();

nothing but a list of falses are the output of this code!

like image 351
Alireza Mirian Avatar asked Aug 11 '12 21:08

Alireza Mirian


People also ask

How do you use QDomDocument?

As @hank commented, you should use QDomDocument::elementsByTagName(const QString &tagname) to get the elements in the document with the name tagname . Then, iterate over the nodes to get each QDomNode . Finally, convert the QDomNode into a QDomElement .

What is QDomDocument?

The QDomDocument class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.


1 Answers

HTML is HTML and XML is XML. Consequently, Qt XML is not able to parse HTML code correctly. To parse HTML files, consider using the Qt Webkit module instead of the Qt XML module. To include it in your project, you just have to add QT += webkit in your project file.

To parse your HTML datas, you will have to do something like this :

QString content = "<html><body><a href='bar'>foo</a></body></html>";
QWebPage webPage;
QWebFrame * frame = webPage.mainFrame();
frame->setHtml(content);
QWebElement htmlElement = frame->documentElement();    // Equivalent of the QDomElement

For further informations, see the Qt Webkit documentation and the QWebElement documentation.

like image 100
air-dex Avatar answered Sep 18 '22 20:09

air-dex