getAttribute Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

getAttribute Method

Gets the value of the attribute.

[Script]

Script Syntax

objValue = oXMLDOMElement.getAttribute(name);

Parameters

name
A string specifying the name of the attribute to return.

Return Value

A variant. Returns the value as a string if the attribute value is a non-empty string. Returns Null if the named attribute does not have a specified value, or an implied default value, such as one taken from a DTD or schema.

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var nodeBook, sIdValue;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   nodeBook = xmlDoc.selectSingleNode("//book");
   sIdValue = nodeBook.getAttribute("id")
   alert(sIdValue);
}
[Visual Basic]

Visual Basic Syntax

objValue = oXMLDOMElement.getAttribute(name)

Parameters

name
A string specifying the name of the attribute to return.

Return Value

A variant. Returns the value as a string if the attribute value is a non-empty string. Returns Null if the named attribute does not have a specified value, or an implied default value, such as one taken from a DTD or schema.

Example

Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeBook As IXMLDOMElement
Dim sIdValue As String
xmlDoc.async = False
xmlDoc.Load "books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set nodeBook = xmlDoc.selectSingleNode("//book")
   sIdValue = nodeBook.getAttribute("id")
   MsgBox sIdValue
End If
[C/C++]

C/C++ Syntax

HRESULT getAttribute(
    BSTR name,
    VARIANT *value);

Parameters

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

C/C++ Return Values

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

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;
}

Remarks

You can also retrieve attributes by using the getNamedItem method of the IXMLDOMNamedNodeMap.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

getNamedItem Method | IXMLDOMNamedNodeMap

Applies to: IXMLDOMElement