次の方法で共有


getAttribute Method1

 

Gets the value of the attribute.

JScript 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.6.0");  
var nodeBook, 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");  
   sIdValue = nodeBook.getAttribute("id")  
   WScript.Echo(sIdValue);  
}  

Output

When used with the sample XML file (books.xml), this example returns "bk101", the value of the id attribute for the first instance of the <book> element in books.xml.

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.

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.

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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXMLDOMElement

See Also

getNamedItem Method
IXMLDOMNamedNodeMap