How to get a specified attribute value using MSXML4
-
How to get the value of "hw" and "pos", xml file:
<?xml version="1.0" ?>
<Root>
<w c5="NN1" hw="factsheet" pos="SUBST">FACTSHEET </w>
<w c5="DTQ" hw="what" pos="PRON">WHAT </w>
<w c5="VBZ" hw="be" pos="VERB">IS </w>
<w c5="NN1" hw="aids" pos="SUBST">AIDS</w>
<w c5="NN1" hw="aids" pos="SUBST">AIDS </w>
<w c5="VVN-AJ0" hw="acquire" pos="VERB">Acquired </w>
</Root>Here is a piece of code:
MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr = pDoc.CreateInstance(\_\_uuidof(MSXML2::DOMDocument40)); if (FAILED(hr)) { MessageBox("err"); return; } pDoc->load("d:\\\\a01.xml"); MSXML2::IXMLDOMNodeListPtr pNodeList; MSXML2::IXMLDOMNode \*DOMNode=NULL; CString strContest; try { pNodeList=pDoc->getElementsByTagName("w"); int j=pNodeList->length; for(int i=0;i<pNodeList->length;++i) {
///////////////////////////////////////////////////////////////
//Here, get the item value, how to get a specified attribute value
pNodeList->get_item(i,&DOMNode);
strContest=(LPCSTR)DOMNode->Gettext();
MessageBox(strContest);
}
} catch(_com_error &err) {
CString strErr=(LPCTSTR)err.Description();
MessageBox(strErr);
} -
How to get the value of "hw" and "pos", xml file:
<?xml version="1.0" ?>
<Root>
<w c5="NN1" hw="factsheet" pos="SUBST">FACTSHEET </w>
<w c5="DTQ" hw="what" pos="PRON">WHAT </w>
<w c5="VBZ" hw="be" pos="VERB">IS </w>
<w c5="NN1" hw="aids" pos="SUBST">AIDS</w>
<w c5="NN1" hw="aids" pos="SUBST">AIDS </w>
<w c5="VVN-AJ0" hw="acquire" pos="VERB">Acquired </w>
</Root>Here is a piece of code:
MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr = pDoc.CreateInstance(\_\_uuidof(MSXML2::DOMDocument40)); if (FAILED(hr)) { MessageBox("err"); return; } pDoc->load("d:\\\\a01.xml"); MSXML2::IXMLDOMNodeListPtr pNodeList; MSXML2::IXMLDOMNode \*DOMNode=NULL; CString strContest; try { pNodeList=pDoc->getElementsByTagName("w"); int j=pNodeList->length; for(int i=0;i<pNodeList->length;++i) {
///////////////////////////////////////////////////////////////
//Here, get the item value, how to get a specified attribute value
pNodeList->get_item(i,&DOMNode);
strContest=(LPCSTR)DOMNode->Gettext();
MessageBox(strContest);
}
} catch(_com_error &err) {
CString strErr=(LPCTSTR)err.Description();
MessageBox(strErr);
}You have to use the
attributes
property (i.e theget_attributes
method) and then thegetNamedItem
on the returned NodeMap, check out theC++
sample of thegetNamedItem
documentation: http://msdn2.microsoft.com/en-us/library/ms767592.aspx[^] it does almost exactly what you need (it retrieves an attribute of the documentElement, but the same applies to a standard node). Please note that you can also do a more intensive use of the smart pointers to make code more concise. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]