这个例子是在根据网上博客《Qt数据库(XML)》改写的一个操作XML的实现。
借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来。
我建立的是Qwidget项目,没有添加ui文件,输出内容都放在应用程序输出中(qDebug)。
XMLtest.pro文件代码:
- #-------------------------------------------------
- #
- #ProjectcreatedbyQtCreator2012-08-15T15:56:54
-
- QT+=coreguixml
- TARGET=XMLtest
- TEMPLATE=app
-
- SOURCES+=main.cpp
- widget.cpp
- HEADERS+=widget.h
widget.h文件代码:
#ifndefWIDGET_H
- #defineWIDGET_H
- #include<QtGui/QWidget>
- #include<QtCore>
- classWidget:publicQWidget
- {
- Q_OBJECT
- public:
- Widget(QWidget*parent=0);
- ~Widget();
- voidread_xml(QStringfilename);
- voidcreate_xml(QStringfilename);
- voidadd_xmlnode(QStringfilename,QStringrmt_name,QStringipa,QStringipb);
- voiddo_xml(constQStringopt,QStringfilename);
- private:
- };
- #endif//WIDGET_H
widget.cpp文件代码:
#include"widget.h"
- #include"qfile.h"
- #include"qdebug.h"
- #include<QDomDocument>
- #include"qtextcodec.h"
- Widget::Widget(QWidget*parent)
- :QWidget(parent)
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
- QFile*file;
- QStringfilename="config.xml";
- if(file->exists("config.xml"))
- read_xml(filename);
- }
- else
- create_xml(filename);
- add_xmlnode(filename,"remote1","127.0.0.1","192.168.1.199");
- do_xml("update",filename);
- Widget::~Widget()
- {
- }
- voidWidget::do_xml( QFilefile(filename);
- if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
- qDebug()<<"openfordoerro";
- file.close();
- QDomDocumentdoc;
- if(!doc.setContent(&file))
- qDebug()<<"setcontentfordoerror";
- file.close();
- QDomNodeListlists=doc.elementsByTagName("remote");
- QDomElementele=lists.at(1).toElement();
- if(ele.attribute(tr("id"))=="3")
- if("delete"==opt||"update"==opt)
- QDomElementroot=doc.documentElement();
- if("delete"==opt)
- root.removeChild(lists.at(1));
- qDebug()<<"removeok!";
- QDomNodeListchild=lists.at(1).childNodes();
- child.at(0).toElement().firstChild().setNodeValue("namechanged");
- child.at(1).toElement().firstChild().setNodeValue("ipachanged");
- child.at(2).toElement().firstChild().setNodeValue("ipbchanged");
- qDebug()<<"modifyok!";
- if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
- qDebug()<<"openforremoveerror!";
- QTextStreamout(&file);
- doc.save(out,4);
- voidWidget::add_xmlnode(QStringfilename,QStringipb)
- if(!file.open(QIODevice::ReadOnly|QFile::Text)){
- qDebug()<<"openforadderror...";
- QDomDocumentdoc;
- QStringerrorStr;
- interrorLine;
- interrorColumn;
- if(!doc.setContent(&file,false,&errorStr,&errorLine,&errorColumn)){
- qDebug()<<"addsetcontenterror...";
-
- QDomElementroot=doc.documentElement();
- if(root.isNull())
- root=doc.createElement("ipconfig");
- QDomElementelement_root=doc.createElement(tr("remote"));
- QDomAttrattr_id=doc.createAttribute(tr("id"));
- QDomElementelement_rmt=doc.createElement(tr("rmt_name"));
- QDomElementelement_ipa=doc.createElement(tr("ipa"));
- QDomElementelement_ipb=doc.createElement(tr("ipb"));
- QStringstr_id;
- if(root.lastChild().isNull())
- str_id="1";
- attr_id.setValue(str_id);
- else
- str_id=root.lastChild().toElement().attribute(tr("id"));
- intcount=str_id.toInt()+1;
- attr_id.setValue(QString::number(count));
- QDomTexttext;
- text=doc.createTextNode(rmt_name);
- element_rmt.appendChild(text);
- text=doc.createTextNode(ipa);
- element_ipa.appendChild(text);
- text=doc.createTextNode(ipb);
- element_ipb.appendChild(text);
- text.clear();
- element_root.appendChild(element_rmt);
- element_root.appendChild(element_ipa);
- element_root.appendChild(element_ipb);
- element_root.setAttributeNode(attr_id);
- root.appendChild(element_root);
- if(!file.open(QIODevice::WriteOnly|QIODevice::Append))
- qDebug()<<"openforadderror!";
- QTextStreamout(&file);
- doc.save(out,4);
- voidWidget::read_xml(QStringfilename)
- qDebug()<<"openforreaderror...";
- QStringerrorStr;
- interrorLine;
- interrorColumn;
- qDebug()<<"setcontenterror...";
- if(root.tagName()!="ipconfig"){
- qDebug()<<"root.tagname!=ipconfig...";
- QDomNodenode=root.firstChild();
- while(!node.isNull())
- if(node.isElement())
- QDomElementelement=node.toElement();
- qDebug()<<qPrintable(element.tagName())<<qPrintable(element.attribute("id"));
- QDomNodeListlist=element.childNodes();
- for(inti=0;i<list.count();i++)
- QDomNodenodechild=list.at(i);
- if(nodechild.isElement())
- qDebug()<<""<<qPrintable(nodechild.toElement().tagName())<<qPrintable(nodechild.toElement().text());
- node=node.nextSibling();
- voidWidget::create_xml(QStringfilename)
- QFilefile(filename);
- file.open(QIODevice::ReadWrite);
- QDomProcessingInstructioninstruction;
- instruction=doc.createProcessingInstruction("xml","version="1.0"encoding="GB2312"");
- doc.appendChild(instruction);
- QDomElementroot=doc.createElement("ipconfig");
- doc.appendChild(root);
- QDomTexttext=doc.createTextNode("");
- root.appendChild(text);
- }
main.cpp文件代码:
#include<QtGui/QApplication>
- #include"widget.h"
- intmain(intargc,char*argv[])
- QApplicationa(argc,argv);
- Widgetw;
- w.show();
- returna.exec();
-
XML文件结构:
<?xmlversion='1.0'encoding='GB2312'?>
- <ipconfig>
- remoteid="1">
- rmt_name>remote1</ipa>127.0.0.1ipb>192.168.1.199remoteremoteid="3">namechanged>ipachanged>ipbchangedremoteid="4"remoteid="5"remoteid="6"remoteid="7"remoteid="8">
应用程序输出:
remote 1
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 3
remote 4
remote 5
remote 6
remote 7
modify ok ! (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|