QT中基于流的XML解析
2011-02-26 18:27
148 查看
今天通过QXmlStreamReader类实现了对XML文件的解析。
实现代码:
对应的XML文件:
在实现的过程中也遇到了一些问题:
1、setDevice(&file)中的参数应为IODevice*型,一般用QFile*赋值,而且这样对应到指定的XML文件很方便。一开始我一直不成功,readNext()返回值为1,invalid,errorStr()的结果是:premature end of document。查了半天资料原来是file要open才行,郁闷啊!因为在QDomDocument类中并没有这样的操作,习惯性思维啊。
2、QXmlStreamReader给我们带来了太大的方便了,readNext()函数就是使劲往下读节点,而name()函数可以获得节点名。知道了自己想要节点的text内容就很方便了,先找到节点,再一个readElementText()就OK了。QXmlStreamReader真的是很方便啊,一个while循环再加if判断就搞定。而我们的QDomDocument就自愧不如了,要一层一层往里面找,写得时候要随时关注自己要处理的XML文件,按照固定套路操作,头都绕晕了,麻烦啊。不知道是我的方法没对还是什么,嘿嘿!
在处理的过程中接触了一个新的有关QString的类QStringRef,没有具体研究,有空研究了在分享。
实现代码:
QString ExeStr[2] = {NULL}; typedef struct _SrcDestPath { QString Content; QString SrcPath; QString DestPath; }SrcDestPath; SrcDestPath Path[100] = {NULL}; int index = -1; int ExeIndex = -1; QXmlStreamReader streamxml; QFile file("E://QT//copy//copy.xml"); file.open(QFile::ReadOnly|QFile::Text); streamxml.setDevice(&file); while(!streamxml.atEnd()) { streamxml.readNext(); if(streamxml.isStartElement()) { if(streamxml.name() == "beforecopy") { ExeIndex = 0; } if(streamxml.name() == "aftercopy") { ExeIndex = 1; } if(streamxml.name() == "copy") { index++; } if(streamxml.name() == "run") { ExeStr[ExeIndex] = streamxml.readElementText(); } if(streamxml.name() == "content") { Path[index].Content = streamxml.readElementText(); } if(streamxml.name() == "source") { Path[index].SrcPath = streamxml.readElementText(); } if(streamxml.name() == "dest") { Path[index].DestPath = streamxml.readElementText(); } } }
对应的XML文件:
<copying> <beforecopy> <run>hello.exe</run> </beforecopy> <copy> <content>content1</content> <source>D:/test/testVFP</source> <dest>D:/test/copy1</dest> </copy> <copy> <content>content2</content> <source>D:/test/WinAPI1/WinAPI1.ncb</source> <dest>D:/test/copy2</dest> </copy> <aftercopy> <run>nihao.exe</run> </aftercopy> </copying>
在实现的过程中也遇到了一些问题:
1、setDevice(&file)中的参数应为IODevice*型,一般用QFile*赋值,而且这样对应到指定的XML文件很方便。一开始我一直不成功,readNext()返回值为1,invalid,errorStr()的结果是:premature end of document。查了半天资料原来是file要open才行,郁闷啊!因为在QDomDocument类中并没有这样的操作,习惯性思维啊。
2、QXmlStreamReader给我们带来了太大的方便了,readNext()函数就是使劲往下读节点,而name()函数可以获得节点名。知道了自己想要节点的text内容就很方便了,先找到节点,再一个readElementText()就OK了。QXmlStreamReader真的是很方便啊,一个while循环再加if判断就搞定。而我们的QDomDocument就自愧不如了,要一层一层往里面找,写得时候要随时关注自己要处理的XML文件,按照固定套路操作,头都绕晕了,麻烦啊。不知道是我的方法没对还是什么,嘿嘿!
在处理的过程中接触了一个新的有关QString的类QStringRef,没有具体研究,有空研究了在分享。
相关文章推荐
- PHP基于事件的XML解析工具类
- 基于sax的xml解析 含源码 UI类(二)
- 基于tinyXml2库封装的解析xml的通用类
- 基于事件的xml文件解析(中国天气网xml解析)
- $.post以及解析xml-基于jQuery的Ajax聊天室程序
- 基于Android XML解析与保存的实现
- 一种基于简单递归思想的易操控xml解析封装(C++)
- ADO.NET实体框架Entity Framework模型-基于XML解析
- 基于dom4j的xml创建与解析
- Python之自动化生成解析XML的C++类(基于tinyxml库解析)
- 基于Dom4J实现XML快速解析(一)
- “基于MFC的第三方XML解析和生成API-------CMarkup类”的一个必须要注意的地方
- PHP Document解析XML(对比基于事件xml解析)
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
- 一个基于XML的传输协议解析
- 基于事件的XML解析【使用解析器】(中国天气网)
- jsp基于XML实现用户登录与注册的实例解析(附源码)
- Android基于Pull方式解析xml的方法详解
- XPathReader------基于XPath的XML解析方式
- jsp基于XML实现用户登录与注册的实例解析(附源码)