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();
}
 

好的,这是一个关于 Qt 的问题。在 Qt 使用 QDomDocument 读写 XML 文件的步骤如下: 1. 创建 QDomDocument 对象,并通过 QDomDocument::setContent() 方法将 XML 文件加载到 DOM。 2. 通过 QDomDocument 对象的 documentElement() 方法获取根节点,并通过 QDomElement 的 elementsByTagName() 方法获取子节点列表。 3. 遍历子节点列表,通过 QDomNode 对象的 toElement() 方法将其转换为 QDomElement 对象,并通过 QDomElement 的 attribute() 方法获取节点的属性值。 4. 通过 QDomElement 的 setAttribute() 方法修改节点的属性值,或通过 QDomDocument 的 save() 方法将 DOM保存XML 文件。 以下是一个简单的示例代码,演示了如何使用 QDomDocument 读取 XML 文件修改节点的属性值: ```cpp #include <QFile> #include <QDomDocument> #include <QDebug> int main() { // 加载 XML 文件到 QDomDocument 对象 QDomDocument doc; QFile file("test.xml"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { doc.setContent(&file); file.close(); } // 获取根节点并遍历子节点列表 QDomElement root = doc.documentElement(); QDomNodeList children = root.elementsByTagName("child"); for (int i = 0; i < children.size(); ++i) { QDomElement child = children.at(i).toElement(); // 获取节点的属性值并输出 QString name = child.attribute("name"); QString value = child.attribute("value"); qDebug() << "Node" << i << "name:" << name << "value:" << value; // 修改节点的属性值 child.setAttribute("value", "new value"); } // 将 DOM保存XML 文件 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << doc.toString(); file.close(); } return 0; } ``` 这段代码加载了名为 test.xmlXML 文件,遍历了其所有名为 "child" 的子节点,并输出了节点的属性值。然后通过 QDomElement 的 setAttribute() 方法修改了其一个节点的属性值,并通过 QDomDocument 的 save() 方法将 DOM保存XML 文件修改后的 XML 文件内容为: ```xml <?xml version="1.0" encoding="UTF-8"?> <root> <child name="node1" value="new value"/> <child name="node2" value="old value"/> </root> ``` 希望这个示例代码可以帮助你了解如何使用 QDomDocumentQt 读取 XML 文件修改节点的属性值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值