Qt下Dom方式修改xml文件的结点值

本文来自他人博客,供学习及参考

=========================================================================================

mainwindow.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDomNode>
#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    bool openXmlFile(QString FilePath);
    bool changeSave();

private:
    Ui::MainWindow *ui;
    QDomDocument m_doc;
};

#endif // MAINWINDOW_H

mainwindow.cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
     changeSave();
}

MainWindow::~MainWindow()
{
    delete ui;
}
bool MainWindow::openXmlFile(QString FilePath)
{
    QFile file( FilePath );
        if( !file.open( QFile::ReadOnly | QFile::Text  ) )
        {
            qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << FilePath;

            return false;
        }

        if( !m_doc.setContent( &file ) )
        {
            qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << FilePath;

            file.close();
            return false;
        }
        file.close();
        return true;
}
bool MainWindow::changeSave()
{
    if(!openXmlFile("/root/a.xml"))
        {
            return false;
        }
        //修改保存xml
    QDomElement root = m_doc.documentElement();
       if(root.tagName()!= "kdevelop")
           return false;
       QDomNode n = root.firstChild();
       while ( !n.isNull() )
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
                       if( e.nodeName() == "general" )
                       {
                           QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
                           for(int a=0; a<list.count(); a++) //遍历该列表
                           {
                               QDomNode node = list.at(a);
                               if(node.isElement())
                               {
                                   if( node.nodeName() == "author" )
                                   {
                                       QDomNode oldnode = node.firstChild();     //标签之间的内容作为节点的子节点出现,得到原来的子节点
                                       node.firstChild().setNodeValue("love");   //用提供的value值来设置子节点的内容
                                       qDebug()<<"after change,the value is"<<node.toElement().text();
                                       QDomNode newnode = node.firstChild();     //值修改过后
                                       node.replaceChild(newnode,oldnode);      //调用节点的replaceChild方法实现修改功能

                                   }
                                   if( node.nodeName() == "email" )
                                   {
                                       QDomNode oldnode = node.firstChild();
                                       node.firstChild().setNodeValue("test@tom.com");
                                       QDomNode newnode = node.firstChild();
                                       node.replaceChild(newnode,oldnode);
                                   }
                               }
                           }
                       }
                   }
           n = n.nextSibling();
             }
             QFile filexml("/root/a.xml");
             if( !filexml.open( QFile::WriteOnly | QFile::Truncate) ){
                 qWarning("error::ParserXML->writeOperateXml->file.open\n");
                 return false;
                    }
                    QTextStream ts(&filexml);
                    ts.reset();
                    ts.setCodec("utf-8");
                    m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
                    filexml.close();
                    return true;


}


xml文件,修改前:

<?xml version="1.0" encoding="UTF-8"?>
<kdevelop>
    <general>
        <author>csdn</author>
        <email>test@tom.com</email>
    </general>
</kdevelop>

xml文件,修改后:

<?xml version="1.0" encoding="UTF-8"?>
<kdevelop>
    <general>
        <author>love</author>
        <email>test@tom.com</email>
    </general>
</kdevelop>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值