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<IXMLDOMNamedNodeMa p> pXMLNodeMap;
CComPtr<IXMLDOMElement> pXMLElement;
CComBSTR bstrNode(L"Message/Connect /CommParm" );
CComBSTR bstrTempNode(L"");
CComBSTR bstrAttribName(L"port");
CComVariant varValue(VT_EMPTY);
TESTHR(pXMLDoc.CoCreateIns tance(__uu idof(DOMDo cument40)) );
VARIANT_BOOL bSuccess = false;
TESTHR(pXMLDoc->load(CComV ariant(L"G :\\Plate room monitor\\Test.xml"), &bSuccess));
pXMLDoc->get_documentEleme nt(&pXMLEl ement);
TESTHR(pXMLElement->get_at tributes(& pXMLNodeMa p));
hr = pXMLNodeMap->getNamedItem( bstrAttrib Name, &pXMLNode);
if (SUCCEEDED(hr) && pXMLNode)
{
TESTHR(pXMLNode->get_nodeV alue(&varV alue));
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.
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<IXMLDOMNamedNodeMa
CComPtr<IXMLDOMElement> pXMLElement;
CComBSTR bstrNode(L"Message/Connect
CComBSTR bstrTempNode(L"");
CComBSTR bstrAttribName(L"port");
CComVariant varValue(VT_EMPTY);
TESTHR(pXMLDoc.CoCreateIns
VARIANT_BOOL bSuccess = false;
TESTHR(pXMLDoc->load(CComV
pXMLDoc->get_documentEleme
TESTHR(pXMLElement->get_at
hr = pXMLNodeMap->getNamedItem(
if (SUCCEEDED(hr) && pXMLNode)
{
TESTHR(pXMLNode->get_nodeV
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.
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!
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
Your post should have been a comment, not an answer.
Regards,
WMB
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(&p XMLNodeLis t);
pXMLNodeList->get_length(& nLen);
I would expect nLen to be 3 but I get the value 1.
Any ideas?
Thanks in advance
If I have the following XML:
<Configuration>
<tag1>
</tag1>
<tag2>
</tag2>
<tag3>
</tag3>
</Configuration>
and the following code:
pXMLDoc->get_childNodes(&p
pXMLNodeList->get_length(&
I would expect nLen to be 3 but I get the value 1.
Any ideas?
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Thank you for advice.
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