Gets the value of the attribute.
objValue = oXMLDOMElement.getAttribute(name);
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.
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);
}
objValue = oXMLDOMElement.getAttribute(name)
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.
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
HRESULT getAttribute(
BSTR name,
VARIANT *value);
name parameter is Null.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;
}
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
in the upper-left corner of the page.
getNamedItem Method | IXMLDOMNamedNodeMap
Applies to: IXMLDOMElement