Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30101051
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2015-09-05 11:16:15

Qt中对于XML文件的写入有两种方式,一个是使用QXmlStreamWriter,另一个则为使用Dom。stream流的形式相对来说更加灵活,而且适合处理大文件。Dom方式由于是将内容加载到了内存中进行操作,所以对于小内存设备则有一定得局限性。

 

下面给出一个用Dom写xml文件的例子。

 

假设定义的节点如下

 


[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <data>  
  3.     <startweek>2010-03-01</startweek>  
  4.     <readnum>3</readnum>  
  5.     <alarm>7</alarm>  
  6.     <prompt>Alarm Window</prompt>  
  7. </data>  
 


则将其写为xml文件时,方法如下:

 


  1. QFile file("write.xml");  
  2. if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate));  
  3. QDomDocument doc;  
  4. QDomProcessingInstruction instruction;  
  5. instruction = doc.createProcessingInstruction("xml","version=/"1.0/" encoding=/"UTF-8/"");  
  6. doc.appendChild(instruction);  
  7. QDomElement root=doc.createElement(tr("data"));  
  8. doc.appendChild(root);  
  9. QDomElement StartWeek=doc.createElement(tr("startweek"));  
  10. QDomElement ReadNumber=doc.createElement(tr("readnum"));  
  11. QDomElement AlarmTime=doc.createElement(tr("alarm"));  
  12. QDomElement AlarmPrompt=doc.createElement(tr("prompt"));  
  13. QDomText text;  
  14. text=doc.createTextNode("2010-03-01");  
  15. StartWeek.appendChild(text);  
  16. text=doc.createTextNode("2");  
  17. ReadNumber.appendChild(text);  
  18. text=doc.createTextNode("10");  
  19. AlarmTime.appendChild(text);  
  20. text=doc.createTextNode("vibration");  
  21. AlarmPrompt.appendChild(text);  
  22. root.appendChild(StartWeek);  
  23. root.appendChild(ReadNumber);  
  24. root.appendChild(AlarmTime);  
  25. root.appendChild(AlarmPrompt);  
  26. QTextStream out(&file);  
  27. doc.save(out,4);  
  28. file.close();  


 

 

QDomProcessingInstruction instruction;

        instruction = doc.createProcessingInstruction("xml","version=/"1.0/" encoding=/"UTF-8/"");

用来写入XML文件的声明,这对于一个XML文件来说不可缺少。

其次写入根节点,然后依次写入相应的子节点。最后从内存写入文件中。

 

 

如果需要用Dom方式对其进行解析,则其为一逆过程,方法如下:

 

 


  1. QDomDocument doc;  
  2.     QFile file("../TEA_Main/files/sysconfig.xml");  
  3.      if (!file.open(QIODevice::ReadOnly))  
  4.          return;  
  5.      if (!doc.setContent(&file)) {  
  6.          file.close();  
  7.          return;  
  8.      }  
  9.      file.close();  
  10.      QDomElement docElem = doc.documentElement();  
  11.      QDomNode n = docElem.firstChild();  
  12.      while(!n.isNull())  
  13.      {  
  14.          QDomElement e = n.toElement(); // try to convert the node to an element.  
  15.          if(!e.isNull())  
  16.          {  
  17.              qDebug()<<e.tagName() << ":" << e.text();  
  18.                
  19.          }  
  20.          n = n.nextSibling();  
  21.      }  
  22.      file.close();  
  23. }  
 


 

使用一个while循环将节点内容读入内存,并根据自己的方式进行处理即可。

 

和前面的QXmlStreamReader解析XML相比,Dom显然简洁很多。

阅读(2330) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~