Microsoft XML SDK 2.6 - XML Reference

IXMLDOMElement::getAttribute Method

Retrieves the value of the named attribute.

Visual Basic Syntax

strValue = oXMLDOMElement.getAttribute(name)

C/C++ Syntax

HRESULT getAttribute(
    BSTR name,
    VARIANT *value);

Parameters

name [in]
Name of the attribute to return.
value [out]
String that contains the attribute value. The empty string is returned if the named attribute does not have a specified or default value.

C/C++ Return Values

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

Remarks

Another way to retrieve attributes is to use the XMLDOMNamedNodeMap object's getNamedItem method.

C/C++ Example

BOOL DOMElementAttribute()
{
   BOOL bResult = FALSE;
   _variant_t varValue;
   BSTR bstrAttributeName = ::SysAllocString(_T("dateCreated"));
   IXMLDOMDocument *pIXMLDOMDocument = NULL;
   IXMLDOMElement *pIXMLDOMElement = NULL;
   HRESULT hr;

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

      If(pIXMLDOMElement)
      {
         varValue = _T("year 2000");
         hr = pIXMLDOMElement->setAttribute(bstrAttributeName, varValue);
         SUCCEEDED(hr) ? 0 : throw hr;

         hr = pIXMLDOMElement->getAttribute(bstrAttributeName, &varValue);
         SUCCEEDED(hr) ? 0 : throw hr;
         if(varValue.vt != VT_NULL)
         {
            ::MessageBox(NULL, _bstr_t(varValue), bstrAttributeName,                   MB_OK);
            bResult = TRUE;
         }
         ::SysFreeString(bstrAttributeName);
         bstrAttributeName = NULL;
         pIXMLDOMElement->Release();
      }
   }
   catch(...)
   {
      if(bstrAttributeName)
         ::SysFreeString(bstrAttributeName);
      if(pIXMLDOMElement)
         pIXMLDOMElement->Release();
      DisplayErrorToUser();
   }
   return bResult;
}

See Also

IXMLDOMElement Interface