ورود

View Full Version : سوال: تجزیه HTML در Qt



AliRezaBeytari
پنج شنبه 13 شهریور 1393, 09:21 صبح
سلام دوستان.
مثلا ما یه QString داریم به این صورت :

QString htmlString(
"<!doctype html>"
"<html>"
"<head>"
"<meta charset='utf-8'>"
"<title>Test Document</title>"
"</head>"
"<body>"
"<div id='test'>Hello World !</div>"
"<body>"
);


حالا میخوام بدونم مثلا چطور میشه title این کد html رو به دست آورد ؟؟؟!!
یا مثلا چطور میشه یه چیزی مثل getElementById براش درست کرد تا با id اون div بشه متن درونش رو کشوند بیرون ؟!!

omid_kma
پنج شنبه 13 شهریور 1393, 18:02 عصر
یا از webkit استفاده کن یا از regex
مثلا این کارایی که گفتی با webkit به این شکل میشه :

#include <QApplication>
#include <QWebElement>
#include <QWebFrame>
#include <QWebPage>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QWebPage webPage;
QWebFrame* frame = webPage.currentFrame();
QString htmlString("<!doctype html>"
"<html>"
"<head>"
"<meta charset='utf-8'>"
"<title>Test Document</title>"
"</head>"
"<body>"
"<div id='test'>Hello World !</div>"
"<body>");
frame->setHtml(htmlString);
QWebElement document = frame->documentElement();
//find title
auto title= document.findFirst("title");
qDebug() << "Title is : " << title.toPlainText();
//find div element
auto items=document.findAll("div");//listi az div haye peida shode
for (auto& item : items)
{
qDebug() <<"id's attribute is :"<< item.attribute("id")<<'\n';
qDebug() << "Tag name is:" << item.tagName() << '\n';
qDebug() << "text : " << item.toPlainText() << '\n';
}
return a.exec();
}

AliRezaBeytari
پنج شنبه 13 شهریور 1393, 19:49 عصر
یا از webkit استفاده کن یا از regex
مثلا این کارایی که گفتی با webkit به این شکل میشه :

#include <QApplication>
#include <QWebElement>
#include <QWebFrame>
#include <QWebPage>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QWebPage webPage;
QWebFrame* frame = webPage.currentFrame();
QString htmlString("<!doctype html>"
"<html>"
"<head>"
"<meta charset='utf-8'>"
"<title>Test Document</title>"
"</head>"
"<body>"
"<div id='test'>Hello World !</div>"
"<body>");
frame->setHtml(htmlString);
QWebElement document = frame->documentElement();
//find title
auto title= document.findFirst("title");
qDebug() << "Title is : " << title.toPlainText();
//find div element
auto items=document.findAll("div");//listi az div haye peida shode
for (auto& item : items)
{
qDebug() <<"id's attribute is :"<< item.attribute("id")<<'\n';
qDebug() << "Tag name is:" << item.tagName() << '\n';
qDebug() << "text : " << item.toPlainText() << '\n';
}
return a.exec();
}



ارور میده ؛ خودتون نگاه کنید :
http://p30up.ir/images/zzf9ortjlpi141lynnp.rar

omid_kma
پنج شنبه 13 شهریور 1393, 19:56 عصر
خط زیر از فایل .pro

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

رو با این جایگزین کن

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets