views:

186

answers:

1

I would like to read and parse certain elements of html files but I'm not interested in rendering it in any way. Basically I would like to go through all my div tags and get some of its style attributes. This is what I've done so far:

QWebPage page;
QWebFrame * frame = page.mainFrame();

QUrl fileUrl("localFile.html");
frame->setUrl(fileUrl);

QWebElement document = frame->documentElement();
QWebElementCollection elements = document.findAll("div");

foreach (QWebElement element, elements){
    std::cout << element.attribute("style").toStdString() << std::endl;
}

Doesn't show anything. I'm somewhat confused if I could use webkits this way. P.D.: I'm using a filechooser to pick the local html root.

+1  A: 

If you don't want to render, why use QWeb* classes? Use simple QFile and maybe QXmlStreamReader?

Kamil Klimek
I had already done it reading characters one by one, it was so ugly I wanted to clean it with Qt. I was hoping that maybe Qt had some high level utilities for html parsing. The QXmlStreamReader seems like a good idea, but for the styles I think I'll need to use regular expressions if I want to do be elegant? That makes me want to stay with my implementation... (afraid face)
Alberto Toglia