Frage

I have a Qt 4.7 program in which I use QWebElement to parse out portions of an HTML file. For example, if I need the portion of the HTML under class "A", I'd run the following code:

QWebView wv;
wv.page()->mainFrame()->setHTML(QString(myStringofHTMLCode);
QWebElement we(wv.page()->mainFrame()->documentElement());
QWebElementCollection elements(we.findAll(".A");

This correctly works and returns everything within any tags with the class "A". Now, however, I have a different problem this doesn't seem to work for. Part of the HTML coming in has table tags, and I need to parse out everything in the table individually within td tags. The problem is, there is no class associated with it; it's just the td tag. I tried running something similar to above, i.e.

QWebElementCollection elements(we.findAll("td");

as the last line, but it doesn't seem to work with built-in tags. Does anyone know how I could get the same functionality as findAll(), except using td instead of a class name? Thanks in advance for any tips!

War es hilfreich?

Lösung

Parse your html content following the example within the QString documentation (http://doc.qt.io/qt-4.8/qstring.html), adapted to your problem:

QString sHtml = QString( myStringofHTMLCode );

int j = 0;

while ( ( j = sHtml .indexOf( "<td>", j ) ) != -1 ) 
{
  int k = sHtml.indexOf( "</td>", j );
  qDebug() << sHtml.mid( j, k ); // your data
  ++j;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top