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;

运行结果如下
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/taozhua/article/details/108565186
今日推荐