Microsoft XML 2.5 SDK


 

IXMLDOMElement::getAttribute Method

[This is preliminary documentation and subject to change.]

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 returning null.

E_INVALIDARG

Value returned if name is null.

Remarks

Another way to retrieve attributes is to use the IXMLDOMNamedNodeMap 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;
}