I finally ended up using libxml2. It does exactly what I wanted.
Here is an example.

Qt Code:
  1. QStringList Core::queryHTML(const QString &html, const QString &query) {
  2.  
  3. htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
  4. htmlDocPtr htmlDoc = htmlCtxtReadMemory(ctxt, html.toUtf8().constData(), strlen(html.toUtf8().constData())
  5. , "", NULL, 0);
  6.  
  7. xmlXPathContextPtr context = xmlXPathNewContext ( htmlDoc );
  8. xmlXPathObjectPtr result = xmlXPathEvalExpression ((xmlChar*) query.toUtf8().constData(), context);
  9. xmlXPathFreeContext (context);
  10. if (result == NULL) {
  11. qDebug()<<"Invalid XQuery ?";
  12. }
  13. else {
  14. xmlNodeSetPtr nodeSet = result->nodesetval;
  15. if ( !xmlXPathNodeSetIsEmpty ( nodeSet ) ) {
  16. for (int i = 0; i < nodeSet->nodeNr; i++ ) {
  17. xmlNodePtr nodePtr;
  18. nodePtr = nodeSet->nodeTab[i];
  19. QString xml = QString::fromUtf8((char*)nodePtr->children->content);
  20. list.append(decodeXml(xml));
  21. }
  22. }
  23.  
  24. xmlXPathFreeObject (result);
  25. }
  26.  
  27. return list;
  28. }
To copy to clipboard, switch view to plain text mode