Link to home
Start Free TrialLog in
Avatar of Gangolf
Gangolf

asked on

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.

<Message>
 <Connect>
  <CommParm port="1" />
 </Connect>
</Message>

Using the following code:

TESTHR(CoInitialize(NULL));
// 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);

TESTHR(pXMLDoc.CoCreateInstance(__uuidof(DOMDocument40)));

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

pXMLDoc->get_documentElement(&pXMLElement);

TESTHR(pXMLElement->get_attributes(&pXMLNodeMap));

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

          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.

Avatar of Wayne Bradney
Wayne Bradney
Flag of United States of America image

Gangolf,

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).

Regards,
WMB
Avatar of MrGhost
MrGhost

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!
MrGhost,

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

Regards,
WMB
Avatar of Gangolf

ASKER

Hi guy's thanks for your input.

If I have the following XML:

<Configuration>
     <tag1>
     </tag1>
     <tag2>
     </tag2>
     <tag3>
     </tag3>
</Configuration>

and the following code:

pXMLDoc->get_childNodes(&pXMLNodeList);
pXMLNodeList->get_length(&nLen);

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

Any ideas?

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Wayne Bradney
Wayne Bradney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MrGhost,

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: https://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.

Netminder
Community Support Moderator
Experts Exchange
Avatar of Gangolf

ASKER

Thank you for advice.