#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
? //document表示對應文檔
? //documentElement取根節(jié)點
? ? QDomElement root = m_doc.documentElement();? ? ? ? //document表示對應文檔
? ? if(root.tagName()!= "kdevelop")
? ? ? ? return false;
? ? QDomNode n = root.firstChild();
? ? while ( !n.isNull() )
? ? {
? ? ? ? //對根節(jié)點可以取得其對應的子節(jié)點
? ? ? ? QDomElement e = n.toElement();
? ? ? ? if( !e.isNull())
? ? ? ? {
? ? ? ? ? ? if( e.nodeName() == "general" )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? QDomNodeList list = e.childNodes(); //獲得元素e的所有子節(jié)點的列表
? ? ? ? ? ? ? ? for(int a=0; a<list.count(); a++) //遍歷該列表
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? QDomNode node = list.at(a);
? ? ? ? ? ? ? ? ? ? if(node.isElement())
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if( node.nodeName() == "author" )
? ? ? ? ? ? ? ? ? ? ? ? {
? ??????????????????????????//標簽之間的內容作為節(jié)點的子節(jié)點出現(xiàn),得到原來的子節(jié)點
? ? ? ? ? ? ? ? ? ? ? ? ? ? QDomNode oldnode = node.firstChild();? ?
? ??????????????????????????//用提供的value值來設置子節(jié)點的內容
? ? ? ? ? ? ? ? ? ? ? ? ? ? node.firstChild().setNodeValue("csdn");? ?
????????????????????????????//值修改過后
? ? ? ? ? ? ? ? ? ? ? ? ? ? QDomNode newnode = node.firstChild();? ??
? ??????????????????????????//調用節(jié)點的replaceChild方法實現(xiàn)修改功能
? ? ? ? ? ? ? ? ? ? ? ? ? ? node.replaceChild(newnode,oldnode);? ? ??
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? 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;
}