64,680
社区成员
发帖
与我相关
我的任务
分享
int parseMessageXMLExample (XercesDOMParser& parser, const String& content, CommandMessage& msg)
{
int nReturn = 0;
bool bError = false;
String msgContent;
msg.m_pAddElementContent.clear();
XERCES_CPP_NAMESPACE::DOMDocument* doc;
XERCES_CPP_NAMESPACE::DOMElement* root;
XERCES_CPP_NAMESPACE::DOMNodeList *nodes;
MemBufInputSource source((const XMLByte*)content.c_str(), content.length(), "HttpRequest", false);
String resource, type;
try
{
parser.resetDocumentPool();
parser.parse(source);
doc = parser.getDocument();
root = doc->getDocumentElement();
if (root==NULL)
{
bError = true;
return WISE_ERR_SERVER_CONNECT_XMLCMDERR;
}
resource = trimString(X(root->getAttribute(X("resource"))));
type = trimString(X(root->getAttribute(X("type"))));
if (resource.size() > 0)
{
msg.m_strSDKResource = resource;
}
else
{
msg.m_nStatus = SC_BAD_REQUEST;
return -1;
}
if (type.size() > 0)
{
msg.m_strSDKType = type;
}
else
{
msg.m_nStatus = SC_BAD_REQUEST;
return -1;
}
if (msg.m_strSDKType == "add")
{
nodes = root->getChildNodes();
for (unsigned int i = 0; i < nodes->getLength(); i++)
{
DOMElement* elem = (DOMElement *)nodes->item(i);
if (elem->getNodeType() == DOMNode::ELEMENT_NODE)
msg.m_pAddElementContent.push_back(elem);
}
}
else
{
nodes = root->getElementsByTagName((const XMLCh *)(X("Content")));
msg.m_cElementContent = (DOMElement*) nodes->item(0);
}
msg.m_pDoc = doc;
}
catch (const XMLException& toCatch)
{
LOG4CXX_ERROR(logger, "Error during parsing xml! " + String(X(toCatch.getMessage())));
LOG4CXX_ERROR(logger, content);
msg.m_nStatus = SC_BAD_REQUEST;
return -1;
}
catch (...)
{
msg.m_nStatus = SC_BAD_REQUEST;
return -1;
}
return 0;
}