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

前端之家收集整理的这篇文章主要介绍了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>
原文链接:https://www.f2er.com/xml/298066.html

猜你在找的XML相关文章