Qt中QDomDocument,读取txt文件和xml文件,并且修改xml文件和保存修改后的文件

此文档main.cpp文件分为两部分:

1】上面是读取txt文档的代码

2】下面是读取和修改xml文档的内容,并且保存xml修改的代码

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDomDocument>
#include <QFile>
#include <QDomNode>
#include <QDebug>
#include <QTextStream>
#include <QDomNodeList>
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
 
    //读取txt文件
//    QFile file("new.txt");
//    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
//        qDebug()<<"open file false";
//    while(!file.atEnd()){
//        QByteArray line=file.readLine();
//        qDebug()<<"line:"<<line;
//    }
 
 
    //读取xml文件
    QDomDocument doc;
    QFile file1("am.xml");
    if (!file1.open(QIODevice::ReadOnly))
    {
        qDebug()<<"open file false";
    }
    QString errorStr;
    int errorLine, errorCol;
 
    if (!doc.setContent(&file1,&errorStr,&errorLine,&errorCol))
    {
        file1.close();
        qDebug()<<"setting file content false";
    }
    file1.close();
    QDomElement docElem = doc.documentElement();
 
    QDomNode n = docElem.firstChild();//
    while (!n.isNull())
    {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull()){
            if(e.nodeName()=="item")
            {
                QDomNodeList list=e.childNodes();
                for(int a=0;a<list.size();a++){
                    QDomNode node=list.at(a);
                    if(node.isElement()){
                        if(node.nodeName()=="num"){
                            QDomNode oldnode=node.firstChild();
                            node.firstChild().setNodeValue("csdn");
                            QDomNode newnode=node.firstChild();
                            node.replaceChild(newnode ,oldnode);
                            qDebug()<<"执行到num里面,修改num值";
                            qDebug()<<"num:"<<node.firstChild().nodeValue();
 
                        }
                        if(node.nodeName()=="hei")
                        {
                            QDomNode oldnode=node.firstChild();
                            node.firstChild().setNodeValue("56");
                            QDomNode newnode=node.firstChild();
                            node.replaceChild(newnode,oldnode);
                            qDebug()<<"执行到hei里面,修改hei值";
                            qDebug()<<node.firstChild().nodeValue();
                        }
                    }
                }
            }
 
        }
        if (!e.isNull())
        {
            qDebug() << qPrintable(e.tagName()) << endl; // the node really is an element.
        }
        n = n.nextSibling();
 
    }
 
    //保存修改后的xml文件
    QFile filexml("am.xml");
    if(!filexml.open(QFile::WriteOnly | QFile::Truncate))
    {
        qWarning("error:ParserXml ->writeXmlOPerator->file.open\n");
        return false;
 
    }
    QTextStream ts(&filexml);
    ts.reset();
    ts.setCodec("utf-8");
    doc.save(ts,2,QDomNode::EncodingFromTextStream);
    filexml.close();
 
    if (engine.rootObjects().isEmpty())
        return -1;
 
    return app.exec();
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值