Qt中修改XML文件中的节点值

丁老师的技术随笔 2012-04-15 06:59:36
不能修改XML文件的节点
xml文件:
<kdevelop>
<general>
<author>zeki</author>
<email>caizhiming@tom.com</email>
</general>
</kdevelop>

我写的源码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtXml>
#include <QDebug>
#include <QFile>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDomDocument dom;
QFile *file=new QFile("/home/qust/qt/XML/2.xml");
if(file->open(QIODevice::ReadOnly|QIODevice::WriteOnly))
{
dom.setContent(file);

}

QDomNodeList email=dom.documentElement().elementsByTagName("email");
qDebug()<<email.count();//打印 1
qDebug()<<email.item(0).toElement().text();//打印caizhiming@tom.com
QDomNode oldnode =email.item(0);

QDomText newnode=dom.createTextNode("99629968@qq.com");
email.at(0).replaceChild(newnode,oldnode);
QTextStream out(file);
dom.save(out,4);
file->close();

}

MainWindow::~MainWindow()
{
delete ui;
}
不能修改XML文件,运行后打开xml文件出现

XML解析错误:废弃 document 元素之后的内容
位置:file:///home/qust/qt/XML/2.xml
行:8,列:1:<?xml version='1.0'?>

^

该怎么修改xml 中节点的值呢??为什么不能使用setNodeValue()方法来修改接点的值???

大家帮帮忙,谢谢!!!
...全文
2607 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
HappyJandun 2013-12-10
  • 打赏
  • 举报
回复
引用 7 楼 l270378034 的回复:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QDialog>
#include <QFile>
#include <QDebug>
#include <QDomDocument>
#include <QFile>
QDomDocument m_doc;
bool  changeSave();
bool openXmlFile(QString FilePath);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    changeSave();
    return a.exec();
}
bool 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  changeSave()
{
    
    if(!openXmlFile("I:/q.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("csdn");   //用提供的value值来设置子节点的内容
                            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("I:/q.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;
}
用到了,谢谢分享
beyondma 2012-04-16
  • 打赏
  • 举报
回复
一般是操作节点的问题.
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

中间是TextNode。你修改它的值。
[/Quote]

setNodeValue 不起作用
myseemydog 2012-04-16
  • 打赏
  • 举报
回复
中间是TextNode。你修改它的值。
hi_52rock 2012-04-16
  • 打赏
  • 举报
回复
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QDialog>
#include <QFile>
#include <QDebug>
#include <QDomDocument>
#include <QFile>
QDomDocument m_doc;
bool changeSave();
bool openXmlFile(QString FilePath);

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
changeSave();
return a.exec();
}
bool 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 changeSave()
{

if(!openXmlFile("I:/q.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("csdn"); //用提供的value值来设置子节点的内容
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("I:/q.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;
}
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

dom.setContent(file);
这一行就出错了吧,你的2.xml是不是有问题?
[/Quote]
2.xml 是我qt写的
代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QTextStream>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QFile *file=new QFile("/home/qust/qt/XML/2.xml");
if(!file->open(QIODevice::WriteOnly|QIODevice::ReadOnly))
{
return ;
}
QDomDocument doc;
QDomText text;
QDomElement element;
QDomProcessingInstruction instruction;
instruction=doc.createProcessingInstruction("xml","version=\'1.0\'");
doc.appendChild(instruction);
QDomElement root=doc.createElement("kdevelop");
doc.appendChild(root);

QDomElement general=doc.createElement("general");
root.appendChild(general);



element=doc.createElement("author");
text=doc.createTextNode("zeki");

element.appendChild(text);
general.appendChild(element);

element=doc.createElement("email");
text=doc.createTextNode("caizhiming@tom.com");
element.appendChild(text);
general.appendChild(element);



QTextStream out(file);
doc.save(out,4);
file->close();


}

MainWindow::~MainWindow()
{
delete ui;
}
FounderSG 2012-04-16
  • 打赏
  • 举报
回复
dom.setContent(file);
这一行就出错了吧,你的2.xml是不是有问题?
  • 打赏
  • 举报
回复
不沉。。

16,815

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧