HRESULT XMLHelper::GetAttribute(MSXML2::IXMLDOMNode *pNode, const TCHAR *pAttr, int *pVal) { MSXML2::IXMLDOMNamedNodeMap *pAttrMap = 0; HRESULT hr = pNode->get_attributes(&pAttrMap); if(hr == S_OK) { BSTR attr = SysAllocString(pAttr); MSXML2::IXMLDOMNode *pAttrNode = 0; hr = pAttrMap->getNamedItem(attr, &pAttrNode); SysFreeString(attr); if(hr == S_OK && pAttrNode) { VARIANTARG varg; VariantInit(&varg); hr = pAttrNode->get_nodeValue(&varg); if(hr == S_OK && varg.vt == VT_BSTR) { *pVal = varg.bstrVal? _wtoi(varg.bstrVal): 0; SysFreeString(varg.bstrVal); } pAttrNode->Release(); } pAttrMap->Release(); } return hr; }
// 获取当前结点的指定属性的值 int CBpXMLNode::GetAttributeByName(const wchar_t *szAttributeName, CString &strAttributeValue) const { if (m_pDOMNode == NULL) return XML_ERROR; MSXML2::IXMLDOMNamedNodeMap *pAttributeMap = NULL; m_pDOMNode->get_attributes(&pAttributeMap); if (pAttributeMap == NULL) return XML_ERROR; MSXML2::IXMLDOMNodePtr pAttNode = pAttributeMap->getNamedItem(szAttributeName); if (pAttNode == NULL) return XML_ERROR; BSTR bstrText = NULL; pAttNode->get_text(&bstrText); strAttributeValue = bstrText; ::SysFreeString(bstrText); return XML_OK; }