I am traversing a DOM using Qt's WebKit classes. Please have a look on the following pseudo HTML:

<br>111<a class="node">AAA</a>
<br>222<a class="node">BBB</a>
...

I can easily find the anchors using findAll(). However I also need to get the text before the elements ("111" and "222"). I tried to use previousSibling() but of course that gives me the <br> element since the "111" and "222" texts are no elements.

I found a function to access text within an element, but how can I access between the <br> and the <a> elements?

有帮助吗?

解决方案

It seems it is not possible. The only workaround I could find is getting the plain text of the parent node and parsing the resulting plain text.

其他提示

This is the way I solved it:

QWebElement *element = ...

// find out if QWebElement has text
QDomDocument doc;
doc.setContent(element->toOuterXml());
QDomElement domelem = doc.documentElement();
for(QDomNode n = domelem.firstChild(); !n.isNull(); n = n.nextSibling())
{
    QDomText t = n.toText();
    if (!t.isNull())
    {
        // it has text !
        qDebug() << t.data();
        break;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top