problem parsing a xml file with MSXML4 in C++

254 views Asked by At

Here is my parsing code:

MSXML2::IXMLDOMNodePtr pNode = m_pXmlDoc->selectSingleNode(kNameOfChild.c_str());

MSXML2::IXMLDOMNodeListPtr pIDOMNodeList = NULL;
MSXML2::IXMLDOMNodePtr pIDOMNode = NULL;
long numOfChildNodes= 0;
BSTR bstrItemText;
HRESULT hr;

MSXML2::IXMLDOMElementPtr pChildNode = m_pXmlDoc->getElementsByTagName(kNameOfChild.c_str());


hr  = m_pXmlDoc->get_childNodes(&pIDOMNodeList);

hr = pIDOMNodeList->get_length(&numOfChildNodes);

And my xml file:

<?xml version="1.0"?>
<GovTalkMessage>
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
  <MessageDetails>
     <Class>MOSWTSC2</Class>
     <Qualifier>acknowledgement</Qualifier>
     <Function>submit</Function>
     <TransactionID>20021202ABC</TransactionID>
     <CorrelationID>B07B9ED3176193DDC4EC39063848A927</CorrelationID>
     <ResponseEndPoint PollInterval="10">
      https://secure.gateway.gov.uk/poll
     </ResponseEndPoint>
     <GatewayTimestamp>2001-01-31T10:20:18.345</GatewayTimestamp>
  </MessageDetails>
  <SenderDetails/>
 </Header>
<GovTalkDetails>
   <Keys/>
</GovTalkDetails>
<Body/>
</GovTalkMessage>
  • kNameOfchild is "Qualifier"
  • pNode is always NULL
  • pChildNode is always NULL
  • hr returns S_OK
  • numOfChildNodes is always 0

So, what am I doing wrong? Thanks

2

There are 2 answers

0
Luke On BEST ANSWER

Try /GovTalkMessage/Header/MessageDetails/Qualifier for the XPath query.

0
Naveen On

You need to provide the xpath for the selectSingleNode call. There may be multiple "Qualifier" present in XML file, so in case you just pass "Qualifier" parser doesn't know which one to return. I haven't used XPath too many times, but I think this string should work for querying: "Header/MessageDetails[0]/Qualifier"