I am writing an application to load xml file and retriev a node value (refer to the xml sample, I am trying to get "memberNumber"), but always get null value.
anyone can hlpe look into the problem ? thanks guys.
Below is my code:
[small]void ProcessMessage(char* strippedMessageData, int messageDataLength)
{
std::cout << "porcess message length="<<messageDataLength<<"\n";
std::cout<< "Received the following message :\n";
std::cout<< &strippedMessageData << "\n";
//load XML message into a Document Object Model
HRESULT result;
// Create the Document Object Model to store the message's XML in
MSXML2::IXMLDOMDocument2Ptr messageDOM;
result = messageDOM.CreateInstance(__uuidof(MSXML2::DOMDocument60));
if (FAILED(result))
{
return;
}
try
{
// Load the message's XML text into the Document Object Model so we can parse it
if(messageDOM->loadXML(strippedMessageData)!= VARIANT_TRUE)
{
return;
}
MSXML2::IXMLDOMElementPtr element = messageDOM->GetdocumentElement();
MSXML2::IXMLDOMNodePtr pParentNode = element->selectSingleNode("ERAContract");
if(pParentNode !=NULL && pParentNode->hasChildNodes() )
{
MSXML2::IXMLDOMNodePtr pChildNode = pParentNode->selectSingleNode("ERAVehicleDetails");
if(pChildNode !=NULL && pParentNode->hasChildNodes() )
{
MSXML2::IXMLDOMNodePtr pChildNode2 = pChildNode->selectSingleNode("memberNumber");
if(pChildNode2 !=NULL )
{
VARIANT varValue;// = pChildNode2->nodeValue;
HRESULT hr1 = pChildNode2->get_nodeValue(&varValue);
//The app always comes here and get S_FALSE result..!!
if(hr1==S_OK)
{
string memberNumber =_bstr_t(varValue);
std::cout<<"--- memberNumber="<<memberNumber<<"\n";
}
}
}
}
}
catch(std::exception e)
{
std::cout <<" exception:" <<e.what()<<"\n";
}
catch(...)
{
}
}[/small]
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms756022(v=vs.85).aspx for an explanation. What you need to do is get the first child of the memberNumber node, and then use get_nodeValue() on that node. Basically, to obtain the text inside the node you obtain the child of that node that is of type NODE_TEXT.