QXmlStreamReader to parse HTML String
Hello,
I'm trying to parse a Html page using QXmlStreamReader but Im stuck somewhere...
I use this method to parse the data
[CODE]void Person::findIt(QNetworkReply *repToRead)
{
QByteArray bytes = repToRead->readAll(); // bytes
QString string(bytes); // string
QXmlStreamReader xml(string);
while (!xml.atEnd()) {
if(xml.readNext()){
qDebug() << xml.name();
if(xml.name() == "head"){
xml.skipCurrentElement();
}
}else if(xml.isEndElement()){
qDebug() << "endElement";
}
}
}[/CODE]
I would like to skip the <head> because of META tags.. and go straight to the body..
I'm getting this in console:
""
""
"html"
""
"head"
and nothing else..how can I bypass the header section?
is there another way to parse html strings in Qt?
Re: QXmlStreamReader to parse HTML String
Solved using:
[CODE]
QByteArray bytes = repToRead->readAll(); // bytes
QWebPage page;
QWebFrame * frame = page.mainFrame();
frame->setContent(bytes);
QWebElement document = frame->documentElement();
QWebElementCollection elements = document.findAll("div.className");
foreach(QWebElement e, elements){
qDebug()<< "e element" << e.toPlainText();
m_numberChecked = e.toPlainText();
emit numberChanged();
}
[/CODE]
Re: QXmlStreamReader to parse HTML String
Hi Francesco,
This problem has been discussed in some other threads here.
The solution you found is actually good and it's the one I prefer the most, but it's makes use of Webkit.
XML parser works only for well formed documents. HTML headers could contain JS scripts or CSS which makes it not XML compliant.
If you want to check where is the problem, you can use [url]http://www.w3schools.com/xml/xml_validator.asp[/url].
QString::remove(QRegExp("<head>.*</head>")) is a way to remove the header tag completely before parsing it.
Re: QXmlStreamReader to parse HTML String
Thank you for your answer :) Im new to Qt and Symbian and I would like to ask you if Webkit it's supported by Symbian^1 or Symbian^3
Re: QXmlStreamReader to parse HTML String
Ciao Francesco,
Webkit is supported by Qt for Symbian. If you have more questions about Qt Smbian related stuff, feel free to ask in the Qt Symbian section.