soap xml 转 json
xml 解析分2中,1种的文档结构树 SAX ,1种是 DOM
如果要求效率的话,使用 pugixml
参考: https://blog.csdn.net/clever101/article/details/7521603
带节点的soap xml 解析成 json
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><PaymentNotification xmlns="http://apilistener.envoyservices.com"><payment><uniqueReference>ESDEUR11039872</uniqueReference> <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference><postingDate>2010-11-15T15:19:45</postingDate><bankCurrency>EUR</bankCurrency><bankAmount>1.00</bankAmount><appliedCurrency>EUR</appliedCurrency><appliedAmount>1.00</appliedAmount><countryCode>ES</countryCode><bankInformation>Sean Wood</bankInformation><merchantReference>ESDEUR11039872</merchantReference></payment></PaymentNotification></soap:Body></soap:Envelope>
解析函数如下:
OrderedMap <string, string> jsonMap;Poco::FileInputStream ifs("K:\\test\\Soapxml.xml"); InputSource src(ifs);try{DOMParser parser;AutoPtr<Document> pDoc = parser.parse(&src);NodeIterator it(pDoc, NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT);Node* pNode = it.nextNode();string strKey, strValue;bool bFindKey = false;while (pNode){const string& key = pNode->nodeName();const string& value = pNode->nodeValue();if (key == "#text") //值{strValue = value; Poco::replaceInPlace(strValue, "\n", "");Poco::trimInPlace(strValue);if (bFindKey){jsonMap[strKey] = strValue;bFindKey = false;}}else //key{if (value.empty()){strKey = key;bFindKey = true;}}pNode = it.nextNode();}}catch (Exception& exc){std::cerr << exc.displayText() << std::endl;}OrderedDynamicStruct aStruct2(jsonMap);std::cout << aStruct2.toString() << endl;
运行结果如下
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<SOAP-ENV:Header>
<cwmp:ID SOAP-ENV:mustUnderstand="1">16051</cwmp:ID></SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:Inform>
<DeviceId>
<Manufacturer>SFU</Manufacturer>
<OUI>74B9EB</OUI>
<ProductClass>EN604-C-RMT</ProductClass>
<SerialNumber>PHXJ-UCZM-LKMS-TTPL</SerialNumber></DeviceId>
<Event xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<EventCode>2 PERIODIC</EventCode>
<CommandKey></CommandKey></EventStruct></Event>
<MaxEnvelopes>1</MaxEnvelopes>
<CurrentTime>0001-01-01T02:52:36</CurrentTime>
<RetryCount>0</RetryCount>
<ParameterList xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="cwmp:ParameterValueStruct[8]"><ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceSummary</Name>
<Value xsi:type="xsd:string">InternetGatewayDevice:1.0[](Baseline:1, IPPing:1)</Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.SpecVersion</Name>
<Value xsi:type="xsd:string">1.0</Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.HardwareVersion</Name>
<Value xsi:type="xsd:string">V2.0</Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name>
<Value xsi:type="xsd:string">V2.0.4-11015</Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name>
<Value xsi:type="xsd:string"></Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
<Value xsi:type="xsd:string">http://192.168.136.99:7547/tr69</Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ParameterKey</Name>
<Value xsi:type="xsd:string"></Value></ParameterValueStruct><ParameterValueStruct>
<Name>InternetGatewayDevice.WANDevice.1.WANConnectionDevice.4.WANIPConnection.1.ExternalIPAddress</Name>
<Value xsi:type="xsd:string">192.168.136.99</Value>
</ParameterValueStruct>
</ParameterList>
</cwmp:Inform>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
解析方法
OrderedMap <string, string> jsonMap;Poco::FileInputStream ifs("K:\\test\\Soapxml.xml"); InputSource src(ifs);try{DOMParser parser;AutoPtr<Document> pDoc = parser.parse(&src);NodeIterator it(pDoc, NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT );Node* pNode = it.nextNode();string strValue;bool bFindST = false;std::vector<string> stVec;while (pNode){const string& strKey = pNode->nodeName();pNode = it.nextNode();ONT_ASSERT_CONTINUE(pNode);strValue = pNode->nodeValue();Poco::replaceInPlace(strValue, "\n", "");Poco::trimInPlace(strValue);if (strKey == "#text" || strKey == "SOAP-ENV:Envelope" || strKey == "SOAP-ENV:Header" || strKey == "SOAP-ENV:Body"){continue;}else if ( strKey == "ParameterValueStruct"){bFindST = true;continue;}if (bFindST){stVec.push_back(strValue);}else{jsonMap[strKey] = strValue;bFindST = false;}}//将ST 的vector 加入到 jsonmap中string strTempKey, strTmpValue;int iSize = stVec.size();for (int i = 0; i < iSize; ++i){if (i % 2 == 0){strTempKey = stVec[i];}else{strTmpValue = stVec[i];jsonMap[strTempKey] = strTmpValue;}}}catch (Exception& exc){std::cerr << exc.displayText() << std::endl;}OrderedDynamicStruct aStruct2(jsonMap);std::cout << aStruct2.toString() << endl;
soap xml 转 json相关推荐
- WebService SOAP XML 与 REST JSON 架构的比较
一个采购订单对10005088物料收货20个,放2050仓库的SOAP XML报文 一般客户端访问服务器端web服务通常可以由HTTPService.WebService.RemoteObject等方 ...
- SOAP XML报文解析
适用于各种 SOAP 和 XML 解析 import java.util.HashMap; import java.util.List; import java.util.Map;import com ...
- xml的数据交换以及xml和json数据交换的比较
一.什么是Web Service ? Web Services 是有企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过internet来访问并使用这项在线服务 WebServicc ...
- XML和JSON瞎掰
XML和JSON瞎掰 XML 跟 JSON 的圣战,或许会成为自 vim/emacs 圣战,cli/gui 圣战等等圣战以来的又一个圣战,而所有的圣战大抵都不会有结果,因为每方都有各自的道理,谁都不服 ...
- XML和JSON数据格式对比
概念 XML 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语 ...
- WebApi接口 - 响应输出xml和json
格式化数据这东西,主要看需要的运用场景,今天和大家分享的是webapi格式化数据,这里面的例子主要是输出json和xml的格式数据,测试用例很接近实际常用情况:希望大家喜欢,也希望各位多多扫码支持和点 ...
- XML和JSON 数据交换格式
为什么80%的码农都做不了架构师?>>> 一.什么是数据交换格式? 客户端与服务器常用数据交换格式xml.json.html 二.数据交换格式应用场景 移动端(安卓.IOS)通 ...
- Java实现xml和json互转
pom.xml: <!-- 使用staxon实现xml和json直接互转 --><dependency><groupId>de.odysseus.staxon< ...
- XML,Object,Json分析转换Xstream采用
XML,Object,Json转换之浅析Xstream的使用 请尊重他人的劳动成果,转载请注明出处:XML,Object,Json转换之浅析Xstream的使用 XStream的是一个简单的库.主要用 ...
- XML转JSON的javascript代码
为什么80%的码农都做不了架构师?>>> 下面是神奇的XML转JSON的javascript代码: // Changes XML to JSON function xmlToJ ...
最新文章
- linux fedora卸载vmware16命令
- 橙子云手机华为保修吗_华为云手机引关注!将替代传统手机?华为回应来了
- 原型的指向是否可以改变 原型最终指向了哪里 原型指向改变如何添加方法和访问
- E - Counting Cliques HDU - 5952
- 【渝粤教育】广东开放大学 文化产业项目案例分析 形成性考核 (51)
- Win10一键修复所有dll缺失的方法
- mysql的临时表空间_Mysql临时表空间详解
- Java随机获取男主角 女主角名字
- 【产品分析】共享充电宝
- CSDN博客微信登录竟然强制扫码关注登录(又来了)
- Python产生100个1—100的随机数放入列表Num中,输出列表中的数,然后将它们排序,并输出排序结果。
- 诚之和:3个月亏17亿,贝壳释放了什么信号?
- 我的世界java版电脑下载,我的世界国际版电脑版下载
- poj 2579 Blurred Vision
- 北京十大推动中国科技发展的人工智能实验室
- Apache Iceberg 数据湖从入门到放弃(2) —— 初步入门
- yii2 框架使用gii工具创建模块
- 滴滴助力2020中国开源年会暨阿帕奇中国路演
- win10如何删除无用服务
- lisp调用qleader端点_超经典【CAD】 lisp程序集锦、【CAD】快捷键大全(好).doc