We help IT Professionals succeed at work.

Newbie: Accessing attributes

Hi Guy's,

Could anyone tell me what I am doing wrong. I am trying to access the attributes of the following XML.

  <CommParm port="1" />

Using the following code:

// Ok parse the XML for relevant data
CComPtr<IXMLDOMDocument> pXMLDoc;
CComPtr<IStream> pStream;
CComPtr<IXMLDOMNode> pXMLNode;
CComPtr<IXMLDOMNodeList> pXMLNodeList;
CComPtr<IXMLDOMNamedNodeMap> pXMLNodeMap;
CComPtr<IXMLDOMElement> pXMLElement;

CComBSTR bstrNode(L"Message/Connect/CommParm");
CComBSTR bstrTempNode(L"");
CComBSTR bstrAttribName(L"port");
CComVariant varValue(VT_EMPTY);


VARIANT_BOOL bSuccess = false;
TESTHR(pXMLDoc->load(CComVariant(L"G:\\Plate room monitor\\Test.xml"), &bSuccess));



hr = pXMLNodeMap->getNamedItem(bstrAttribName, &pXMLNode);
if (SUCCEEDED(hr) && pXMLNode)
     if (varValue.vt == VT_BSTR)

          LPTSTR tMsg = W2T(varValue.bstrVal);
          MessageBox(NULL, tMsg, _T("Node Value"), MB_OK);

It seems to be Ok until it gets to getNamedItem() which returns S_FALSE and pXMLNode == NULL.

Any ideas?

Also using the above method how would use the IStream interface to write streamed data to the DOC. I can do it by using conventional CoCreateInstance() and IXMLDOMDOcument but not with the CComPtr notation.

Thanks in advance.

Watch Question

Wayne BradneyIndependent Consultant


If I'm reading you code correctly, you're getting the documentElement (pXMLElement) and then asking for the attributes of it (pXMLNodeMap).

The documentElement of your XML document is <Message>, which has no attributes.

If you want the attributes of your <CommParm> element, you'll need to navigate further into the DOM (see getChildNodes).


heheh you are getting low on points ;)

ok you can use getElementsByTagName to get list of nodes then when you found your node in case if you have more then one use pIDOMNodeList->get_item then call get_attributes and then get_item on your IXMLDOMNamedNodeMap!

i hope this helps!
Wayne BradneyIndependent Consultant


Your post should have been a comment, not an answer.



Hi guy's thanks for your input.

If I have the following XML:


and the following code:


I would expect nLen to be 3 but I get the value 1.

Any ideas?

Thanks in advance
Independent Consultant

My guess is that pXMLDoc refers to the Document itself, which has one child, the <Configuration> element. Use getDocumentElement to get to the <Configuration> element, the your call to getChildNodes will probably return 7:

1: Whitespace (TEXT_NODE)
2: <tag1> (ELEMENT_NODE)
3: Whitespace (TEXT_NODE)
4: <tag2> (ELEMENT_NODE)
5: Whitespace (TEXT_NODE)
6: <tag3> (ELEMENT_NODE)
7: Whitespace (TEXT_NODE)


You've been asked politely in several questions to refrain from posting your suggestions as Answers. Experts Exchange asks you to do so because it is a collaborative site; posting as Answers inhibits that process by moving the question from the Open list to the Locked list.

Please read the following guidelines regarding what is a Comment and what is an Answer: http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

More importantly, you've replied to the requests of Experts that you adhere to this set of norms with unprofessional and inappropriate remarks.

The following is from the Membership Agreement, to which you are a party:

"Experts Exchange reserves the right to determine what constitutes Internet abuse and objectionable conduct. If Experts Exchange believes, in its sole discretion, that a violation of these Guidelines has occurred, it may take responsive action. Such action may /include, but not be limited to, permanent removal of illegal or inappropriate information or content, or suspension or termination of your membership and access to Experts Exchange Service..."

"...* posting, transmitting or linking to profane language, descriptions of situations or scenarios considered, in the opinion of Experts Exchange, inappropriate for the Experts Exchange membership..."

"...* spamming, flaming or other similar activities..."

"...* violating the guidelines for academic honesty or other unethical behavior..."

I hope this clears up any misunderstanding you might have about this site.

Community Support Moderator
Experts Exchange


Thank you for advice.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.