QT XML文件 修改节点
来源:互联网 发布:积分月结数据库设计 编辑:程序博客网 时间:2024/06/11 22:25
感谢:l270378034的帮助
源xml文件:
<kdevelop> <general> <author>zeki</author> <email>caizhiming@tom.com</email> </general></kdevelop>
源程序:
#include "mainwindow.h"#include "ui_mainwindow.h"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("/home/qust/qt/XML/2.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("/home/qust/qt/XML/2.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;}
运行后:
<kdevelop> <general> <author>csdn</author> <email>test@tom.com</email> </general></kdevelop>
QDomNode QDomNode::nextSibling () const
Returns the next sibling in the document tree. Changing the returned node will also change the node in the document tree.
If you have XML like this:
<h1>Heading</h1> <p>The text...</p> <h2>Next heading</h2>
and this QDomNode represents the <p> tag, nextSibling() will return the node representing the <h2> tag.
sibling 1.同层级 2.同辈 3.兄弟或姊妹。 4.同胞 Sibling 1.同胞兄妹
- QT XML文件 修改节点
- asp 修改xml文件节点内容
- XML修改节点属性及写入文件
- 修改xml文件的节点属性
- 修改XML文件的节点属性值
- java操作xml文件--修改节点
- 修改xml文件中节点的值
- Qt下Dom方式修改xml节点的方法
- Qt Xml 添加节点
- Qt的XML封装类QXmlWrapper,实现XML文件的创建、删除以及元素节点的增加、删除、修改与查找等。
- 修改xml节点。
- 修改XML节点名称
- C#中操作xml文件(插入节点、修改、删除)
- C#中操作xml文件(插入节点、修改、删除)
- C# 生成xml文件,新增,修改,删除节点
- C# xml文件的创建,修改和添加节点
- C#中操作xml文件(插入节点、修改、删除)
- C# xml文件的创建,修改和添加节点 。
- 关于android R.layout 中找不到已存在的布局文件问题的解决
- Linux的man那点事
- 怎样成为优秀的电子工程师
- Android Layout XML属性
- Java输入输出流详解
- QT XML文件 修改节点
- 在VB.NET中 用代码 利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法
- vi/vim常用命令
- TCP原理学习笔记
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- linux x86 ELF 反汇编
- linux-101hacks 之 sort
- tomcat6启动失败 命令窗口一闪而过
- [分享]学历与学习力