设为首页 加入收藏

TOP

C++请求Web Service与XML解析(一)
2015-02-13 18:24:25 来源: 作者: 【 】 浏览:39
Tags:请求 Web Service XML 解析

1. C++解析XML的开源库


在项目中XML的解析使用的是开源的第三方库,TinyXML;这个解析库的模型通过XML文件,然后再内存中生成DOM模型,从而让我们可以很方便的遍历这颗XML树。


DOM模型即文档对象模型,是将整个文档分成多个元素(如:书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。先看一下TinyXML中的主要类和XML文档之间的对应关系,下图是TinyXML中主要class的类图,反应各个类之间的静态关系。


C语言梳理一下,分布在以下10个章节中:


TiXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承自TiXMLBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode基础上又衍生出来这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<...***=...>,其中***就是一个属性,这里采用一个XML文档具体说明一下:


1.?
2.?
3.? ? ?
4.? ? ?
5.? ? ? ? miaomaio?
6.? ? Shaanxi Xi'an?
7.? ? 13759911917?
8.? ? miaomiao@home.com?
9.? ?
?
10.? ? ?
11.? ? ? ? gougou?
12.? ? Liaoning Shenyang?
13.? ? 15840330481?
14.? ? gougou@home.com?
15.? ?
?
16.? ? ?
16.?
?


1) 读取XML文件


//!xml文件读取,模拟调用接口返回char *字符串
?filebuf *pbuf;
?ifstream filestr;
?long size;
?char *buffer;


?filestr.open(XML_EXAMPLE_FILE_NAME, ios::binary);
?pbuf = filestr.rdbuf();
?size = pbuf->pubseekoff(0,ios::end,ios::in);
?pbuf->pubseekpos(0, ios::in);


?buffer = new char[size];
?pbuf->sgetn(buffer, size);
?filestr.close();


?//!从xml字符串中获取相关值
?string strCreationTime;
?string strJobId;
?string strJobType;
?string strJobName;
?string strJobLeader;


?TiXmlDocument *xmlDocument = new TiXmlDocument();
?xmlDocument->Parse(buffer, 0, TIXML_DEFAULT_ENCODING);


?TiXmlElement *RootElement = xmlDocument->RootElement();
?TiXmlElement *fileHeaderElement = RootElement->FirstChildElement();
?TiXmlElement *fileBodyElement = fileHeaderElement->NextSiblingElement();


?for (TiXmlElement *nodeElement = fileHeaderElement->FirstChildElement(); nodeElement; nodeElement = nodeElement->NextSiblingElement())
?{
? string strElementKey = nodeElement->Value();
? if (strElementKey.compare(XML_PARSE_CREATION_TIME) == 0)
? {
? ?strCreationTime = nodeElement->GetText();
? }
?}


?for (TiXmlElement *nodeElement = fileBodyElement->FirstChildElement(); nodeElement; nodeElement = nodeElement->NextSiblingElement())
?{
? string strElementKey = nodeElement->Value();
? if (strElementKey.compare(XML_PARSE_JOB_ID) == 0)
? {
? ?strJobId = nodeElement->GetText();
? }


? if (strElementKey.compare(XML_PARSE_JOB_TYPE) == 0)
? {
? ?strJobType = nodeElement->GetText();
? }


? if (strElementKey.compare(XML_PARSE_JOB_NAME) == 0)
? {
? ?strJobName = nodeElement->GetText();
? }


? if (strElementKey.compare(XML_PARSE_JOB_LEADER) == 0)
? {
? ?strJobLeader = nodeElement->GetText();
? }
?}


2) 创建、生成XML文件
以如下XML文件为例:




? ?
? ? ? ? ? ProxyMiddleWareJobSearch
? ? ? ? ? WetLand
? ? Platform
? ? ? ? ? 2014-9-18 10:25:20
? ?

? ?
? ? ? ? ? ...
? ? ? ? ? 13
? ? ? ? ?
? ?


?


//! 构建XML字符串
?TiXmlDocument *pDoc = new TiXmlDocument;
?TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0", "gb2312", "");
?pDoc->LinkEndChild(pDeclaration);


?TiXmlElement *pEleRoot = new TiXmlElement("InterFaceFile");
?pDoc->LinkEndChild(pEleRoot);


?TiXmlElement *pEleFileHeader = new TiXmlElement("FileHeader");
?TiXmlElement *pEleFileBody = new TiXmlElement("FileBody");
?pEleRoot->LinkEndChild(pEleFileHeader);
?pEleRoot->LinkEndChild(pEleFileBody);


?TiXmlElement *pEleMessageType = new TiXmlElement("MessageType");
?TiXmlElement *pEleOriginator = new TiXmlElement("Originator");
?TiXmlElement *pEleRecipient = new TiXmlElement("Recipient");
?TiXmlElement

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇RPC通信框架——RCF介绍 下一篇C++类的实例化对象的大小之sizeof..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: