Qtで「XML」を読み込む手帳

ウェブからRSS(XML)を読み込む。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    view(0),
    contents("")
{
    ui->setupUi(this);
    view = new QWebView(this);
    setCentralWidget(view);

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);

    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://rss.sina.com.cn/roll/sports/hot_roll.xml")));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        QDomDocument doc;
        doc.setContent(reply->readAll());

        channel = doc.firstChildElement().firstChildElement();
        channelContent = channel.childNodes();
        contents += "<style>.item{background:#f8f8f8; font-weight:bold; font-family: arial; margin:10px; font-size:14px;padding:5px;}.item:hover{background:#36b0f3;} .description:hover{color:#fff;} .title{color:#666;white-space:nowrap;} .description{color:#000; margin:0px 5px;}</style>";
        for(unsigned int i = 0; i < channelContent.count(); i++)
        {
            if(channelContent.at(i).toElement().tagName() == "title")
            {
                //qDebug()<<"Element"<<channelContent.at(i).toElement().text();
                contents += ("<div style=" font-weight:bold; font-family: arial; font-size:16px; text-align:center; height:20px; overflow:hidden;">"+channelContent.at(i).toElement().text()+"</div>");
            }
            else if(channelContent.at(i).toElement().tagName() == "item")
            {
                contents += "<div class="item">";
                channelContentList = channelContent.at(i).childNodes();
                for(unsigned int j = 0; j < channelContentList.count(); j++)
                {

                    if(channelContentList.at(j).toElement().tagName() == "title")
                    {
                        qDebug()<<"Element"<<channelContentList.at(j).toElement().text();
                        contents += ("<div class="title">"+channelContentList.at(j).toElement().text()+"</div>");
                    }
                    else if(channelContentList.at(j).toElement().tagName() == "description")
                    {
                        qDebug()<<"Description"<<channelContentList.at(j).toElement().text();
                        contents += ("<div class="description">"+channelContentList.at(j).toElement().text()+"</div>");
                    }
                    else if(channelContentList.at(j).toElement().tagName() == "pubDate")
                    {
                        qDebug()<<"Description"<<channelcontentlist at="" j="" toelement="" text="" ui-="">webView->append(""+channelContentList.at(j).toElement().text()+"");
                    }
                    else
                    {

                    }
                }
                contents += "</channelcontentlist></div>";
            }
        }
        view->setHtml(contents);
        //qDebug()<<channelContent.count();
    }
}
</qdebug>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值