#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <xercesc/dom/DOMLSOutput.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMAttr.hpp>
#include <xercesc/dom/DOMLSSerializerFilter.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMLSException.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/dom/DOMLSSerializer.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/util/BinInputStream.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/dom/DOMLSParserFilter.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <iostream>
using namespace XERCES_CPP_NAMESPACE;
using namespace std;
static const char* gMemBufId = "prodInfo";
static char g_newNodeName[4][20]={"bal_ye","bal_ky","bal_kq","bal_dj"};
static char g_typeValue[4][10] = {"0","1","2","3"};
extern "C" bool DoRecving(char* pDatabuf, int iLen,char* pNewBuf,int* iRetLen,int iNewLen);
extern "C" void RemoveNode(DOMDocument* pDoc,DOMElement* tagNode);
extern "C" void RemoveNode(DOMDocument* pDoc,DOMElement* tagNode)
{
if (tagNode == NULL|| pDoc == NULL)
{
return ;
}
DOMElement* newElementNode;
//DOMText* newTextNode;
DOMElement* typeNode = tagNode->getFirstElementChild();
DOMElement* balNode = typeNode->getNextElementSibling();
char* typeValue = XMLString::transcode(typeNode->getTextContent());
for (int i=0;i<4;i++)
{
XMLCh* newNodeName = XMLString::transcode(g_newNodeName[i]);
if (strcmp(typeValue,g_typeValue[i])==0)
{
newElementNode=pDoc->createElement(newNodeName);
XMLString::release(&newNodeName);
break;
}
XMLString::release(&newNodeName);
}
DOMNode* parentNode = tagNode->getParentNode();
newElementNode->appendChild(typeNode);
newElementNode->appendChild(balNode);
parentNode->insertBefore(newElementNode,tagNode->getNextElementSibling());
parentNode->removeChild(tagNode);
return ;
}
extern "C" void Work(DOMDocument* pDoc,DOMElement * element)
{
while(element)
{
DOMElement* nextNode;
char* elementName = XMLString::transcode(element->getTagName());
if (strcmp(elementName,"ScBal")==0)
{
nextNode = element->getNextElementSibling();
RemoveNode(pDoc,element);
element = nextNode;
}
else if(elementName,"MsgHdr")
{
DOMElement* elementChild = element->getFirstElementChild();
while(elementChild)
{
elementName = XMLString::transcode(elementChild->getTagName());
if (strcmp(elementName,"InstrCd")==0)
{
char* tranCode = XMLString::transcode(elementChild->getTextContent());
if (strcmp(tranCode,"11008") != 0)
{
XMLString::release(&tranCode);
return ;
}
else
{
break;
}
}
elementChild = elementChild->getNextElementSibling();
}
element = element->getNextElementSibling();
}
else
{
element = element->getNextElementSibling();
}
XMLString::release(&elementName);
}
}
extern "C" DOMDocument* LoadMem(MemBufInputSource* pMemBufIS,XercesDOMParser* pParser,const char* pDatabuf)
{
MemBufInputSource* memBufIS = new MemBufInputSource
(
(const XMLByte*)pDatabuf
, strlen(pDatabuf)
, gMemBufId
, false
);
pMemBufIS = memBufIS;
XercesDOMParser* parser = new XercesDOMParser;
try
{
parser->parse(*memBufIS);
}
catch (SAXException& e)
{
printf("msg:%s\n", XMLString::transcode(e.getMessage()));
return NULL;
}
catch (XMLException& e)
{
printf("code:%d, msg:%s\n", e.getCode(), XMLString::transcode(e.getMessage()));
return NULL;
}
catch (DOMException& e)
{
printf("code:%d, msg:%s\n", e.code, e.msg);
return NULL;
}
DOMDocument* pDoc = NULL;
try
{
pDoc = parser->getDocument();
if(pDoc == NULL)
{
printf("pDoc fail \n");
}
}
catch (DOMLSException& e)
{
printf("code:%d, msg:%s\n", e.code, e.msg);
return NULL;
}
catch (DOMException& e)
{
printf("code:%d, msg:%s\n", e.code, e.msg);
return NULL;
}
return pDoc;
}
extern "C" bool DoRecving(char* pDatabuf, int iLen,char* pNewBuf,int* iRetLen,int iNewLen)
{
string shead = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
printf("\n%s\n",pDatabuf);
printf("%d",strlen(pDatabuf));
XMLPlatformUtils::Initialize();
MemBufInputSource* pMemBufIS = NULL;
XercesDOMParser* pParser = NULL;
DOMDocument* pDoc =LoadMem(pMemBufIS,pParser,pDatabuf);
DOMElement* root = pDoc->getDocumentElement();
if (root == NULL)
{
delete pMemBufIS;
delete pParser;
pMemBufIS = NULL;
pParser = NULL;
shead.append(pDatabuf);
pDoc = LoadMem(pMemBufIS,pParser,shead.c_str());
}
root = pDoc->getDocumentElement();
if(root == NULL)
{
delete pMemBufIS;
delete pParser;
pMemBufIS = NULL;
pParser = NULL;
return false;
}
DOMElement* firstNode = root->getFirstElementChild();
DOMElement* temNode = firstNode;
for (int i=0;i<2;i++)
{
temNode = temNode->getFirstElementChild();
}
Work(pDoc,temNode);
try
{
XMLCh tempStr[3] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
//TranscodeToStr transcodeToStr(theSerializer->writeToString(root),"gbk",XMLPlatformUtils::fgMemoryManager);
const char* gbk = "GBK";
char* data = (char*)TranscodeToStr(theSerializer->writeToString(root),gbk).adopt();
//
char datalen[10]={0};
//
int ndatalen = strlen(data);
printf("\nempty = %s\n",data);
printf("\n*******ndatalen = %d\n",ndatalen);
sprintf(datalen,"%05d",ndatalen);
firstNode = pDoc->getDocumentElement();
firstNode->setAttribute( XMLString::transcode("Len"),XMLString::transcode(datalen));
MemBufFormatTarget *myFormTarget;
myFormTarget = new MemBufFormatTarget(2000, XMLPlatformUtils::fgMemoryManager);
theOutputDesc->setByteStream(myFormTarget);
theSerializer->write(pDoc, theOutputDesc);
XMLFormatter *formatter = new XMLFormatter("gbk",myFormTarget);
const XMLByte* toWrite = new XMLByte[2000];
toWrite = myFormTarget->getRawBuffer();
memcpy(pNewBuf,toWrite,strlen((char*)toWrite));
*iRetLen = strlen(pNewBuf);
printf("\n====pNewBuf = %s\n",pNewBuf);
theOutputDesc->release();
theSerializer->release();
XMLString::release(&data);
delete formatter;
delete myFormTarget;
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
return false;
}
catch (XMLException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during creation of output transcoder. Msg is:"
<< XERCES_STD_QUALIFIER endl
<< XMLString::transcode(e.getMessage()) << XERCES_STD_QUALIFIER endl;
return false;
}
delete pMemBufIS;
delete pParser;
return true;
}
xerces解析器的使用实例详解
最新推荐文章于 2021-03-20 05:41:29 发布