Export (0) Print
Expand All
DOM
Expand Minimize

getAttributeNode Method

Gets the attribute node.

var objXMLDOMAttribute = oXMLDOMElement.getAttributeNode(name);

Parameters

name

The string specifying the name of the attribute to be retrieved.

Return Value

An object. Returns IXMLDOMAttribute with the supplied name, or Null if the named attribute cannot be found on this element.

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var nodeBook, nodeId, sIdValue;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   nodeBook = xmlDoc.selectSingleNode("//book");
   nodeId = nodeBook.getAttributeNode("id");
   sIdXml = nodeId.xml;
   WScript.Echo(sIdXml);
}

Output

When used with the sample XML file (books.xml), this example returns the full attribute xml string of:

id="bk101"

for the id attribute for the first instance of the <book> element in books.xml.

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

Parameters

name[in]

The name of the attribute to be retrieved.

attributeNode[out, retval]

An IXMLDOMAttribute object that is returned with the supplied name, or Null if the named attribute cannot be found on this element.

Return Values

S_OK

The value returned if successful.

S_FALSE

The value returned when no attribute with the given name is found.

E_INVALIDARG

The value returned if name is Null.

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

Implemented in: MSXML 3.0 and MSXML 6.0

Show:
© 2014 Microsoft