IXMLDOMAttribute

MSXML 5.0 SDK

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

IXMLDOMAttribute

Represents an attribute of the IXMLDOMElement. Valid and default values for the attribute are defined in a document type definition (DTD) or schema.

[Script]

Example

The following script example creates an IXMLDOMAttribute object from the first attribute of the root and displays the object's value and xml properties.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var root;
var objDOMatt;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   root = xmlDoc.documentElement;
   objDOMatt = root.firstChild.attributes.item(0);
   alert(objDOMatt.value);
   alert(objDOMatt.xml);
}
[Visual Basic]

Example

The following Microsoft® Visual Basic® example creates an IXMLDOMAttribute object from the first attribute of the root and displays the object's value and xml properties.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim root As IXMLDOMElement
Dim objDOMatt As IXMLDOMAttribute
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 root = xmlDoc.documentElement
   Set objDOMatt = root.firstChild.Attributes.Item(0)
   MsgBox (objDOMatt.Value)
   MsgBox (objDOMatt.xml)
End If
[C/C++]

Example

#import "msxml5.dll"
using namespace MSXML2;

#define CHECK_AND_RELEASE(pInterface)  \
if(pInterface) \
{\
pInterface->Release();\
pInterface = NULL;\
}\

#define RELEASE(pInterface)  \
{\
pInterface->Release();\
pInterface = NULL;\
}\

int main(int argc, char* argv[])
{
_variant_t varValue;
BSTR bstrAttributeName = ::SysAllocString(_T("dateCreated"));
IXMLDOMAttribute *pIXMLDOMAttribute = NULL;
IXMLDOMElement *pIXMLDOMElement = NULL;
IXMLDOMDocument *pIXMLDOMDocument = NULL;
HRESULT hr;

try
{
   // Initialize pIXMLDOMDocument and load an
   // XML file into it.
   // Get the document root.
   hr = pIXMLDOMDocument->get_documentElement( &pIXMLDOMElement);
   SUCCEEDED(hr) ? 0 : throw hr;
   if(pIXMLDOMElement)
   {
      varValue = _T("Year 2000");

      // Set the "dateCreated" attribute to "year 2000"
      hr = pIXMLDOMElement->setAttribute(bstrAttributeName, varValue);
      SUCCEEDED(hr) ? 0 : throw hr;

      // Retrieve the changed attribute.
      hr = pIXMLDOMElement->getAttributeNode(bstrAttributeName,
                                             &pIXMLDOMAttribute);
      SUCCEEDED(hr) ? 0 : throw hr;

      pIXMLDOMAttribute->get_nodeValue(&varValue);
     if(varValue.vt != VT_NULL)
      {
         // Display the retrieved attribute in a message box.
         ::MessageBox(NULL, _bstr_t(varValue), bstrAttributeName, MB_OK);
      }
      RELEASE(pIXMLDOMElement);
     RELEASE(pIXMLDOMAttribute);
   }
   ::SysFreeString(bstrAttributeName);
   bstrAttributeName=NULL;
}
catch(...)
{
   if(bstrAttributeName)
   {
      ::SysFreeString(bstrAttributeName);
      bstrAttributeName = NULL;
   }
   CHECK_AND_RELEASE(pIXMLDOMElement);
//   DisplayErrorToUser();
}
// Release pIXMLDOMDocument when finished with it.
   return 0;
}

Output (in a message box)

Year 2000

Remarks

Attribute nodes cannot be the immediate children of IXMLDOMDocumentFragment. However, they can be associated with element nodes that are children of IXMLDOMDocumentFragment node.

The relationship between the value and specified members can be summarized as follows: If the attribute has an assigned value in the document and the specified property is True, the value is the assigned value. If the attribute has no assigned value in the document and has a default value in the DTD, the specified property is False and the value is the default value in the DTD. If the attribute has no assigned value in the document and has a value of #IMPLIED in the DTD, the attribute does not appear in the structure model of the document.

In XML, the child nodes of the attribute node represent the value of an attribute because the value can contain entity references. Thus attributes that contain entity references will have a child list containing both text nodes and entity reference nodes. In addition, because the attribute type might be unknown, there are no tokenized attribute values.

[C/C++]

IXMLDOMAttribute inherits IXMLDOMNode but are not actually child nodes of the element and are not considered part of the document tree. Attributes are considered members of their associated elements rather than independent and separate. Thus IXMLDOMAttribute parentNode, previousSibling, and nextSibling members have the value Null.

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

Header and IDL files: msxml2.h, msxml2.idl

Inherits From: IXMLDOMNode

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.

Versioning

MSXML 2.0 and later

See Also

IXMLDOMAttribute Members | IXMLDOMElement | IXMLDOMDocumentFragment | value Property | specified Property | IXMLDOMNode | parentNode Property | previousSibling Property | nextSibling Property