Microsoft XML SDK 2.6 - XML Reference

IXMLDOMElement::getAttributeNode Method

Retrieves the named attribute node.

Visual Basic Syntax

Set objXMLDOMAttribute = oXMLDOMElement.getAttributeNode(name)

C/C++ Syntax

HRESULT getAttributeNode(
    BSTR name,
    IXMLDOMAttribute **attributeNode);

Parameters

name [in]
Name of the attribute to be retrieved.
attributeNode [out]
DOMAttribute object that is returned with the supplied name, or Null if the named attribute cannot be found on this element.

C/C++ Return Values

S_OK
Value returned if successful.
S_FALSE
Value returned when no attribute with the given name is found.
E_INVALIDARG
Value returned if name is Null.

C/C++ Example

BOOL DOMElementAttributeNode()
{
   BOOL bResult = FALSE;
   BSTR bstrAttributeName = ::SysAllocString(_T("dateCreated"));
   IXMLDOMAttribute* pIXMLDOMAttribute = NULL;
   IXMLDOMElement *pIXMLDOMElement = NULL;
   IXMLDOMDocument *pIXMLDOMDocument = NULL;
   HRESULT hr;

   try
   {
      // Create an instance of DOMDocument and initialize 
      // pIXMLDOMDocument.
      // Load/create an XML fragment.
      hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
      SUCCEEDED(m_hr) ? 0 : throw hr;

      If(pIXMLDOMElement)
      {
         hr = pIXMLDOMElement->getAttributeNode(bstrAttributeName,                   &pIXMLDOMAttribute);
         if(SUCCEEDED(hr) && pIXMLDOMAttribute)
         {
         // Read node value and display . . .
            bResult = TRUE;
            pIXMLDOMAttribute->Release();
            pIXMLDOMAttribute = NULL;
         }
         ::SysFreeString(bstrAttributeName);
         bstrAttributeName = NULL;
         pIXMLDOMElement->Release();
         pIXMLDOMElement = NULL;
      }
   }
   catch(...)
   {
      if(bstrAttributeName)
         ::SysFreeString(bstrAttributeName);
      if(pIXMLDOMAttribute)
         pIXMLDOMAttribute->Release();
      if(pIXMLDOMElement)
         pIXMLDOMElement->Release();
      DisplayErrorToUser();
   }
   return bResult;
}

See Also

IXMLDOMElement Interface