getNamedItem Method Retrieves the attribute with the specified name.[Script] Script Syntax var objXMLDOMNode = oXMLDOMNamedNodeMap.getNamedItem(name); Parameters name String specifying the name of the attribute. Return Value Object. Returns IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.Example var xmlDoc = new ActiveXObject("Msxml2.DOMDocument"); var nodeBook, nodeId; xmlDoc.async = false; xmlDoc.load("books.xml"); nodeBook = xmlDoc.selectSingleNode("//book"); nodeId = nodeBook.attributes.getNamedItem("id"); alert(nodeId.value); [Visual Basic] Visual Basic Syntax Set objXMLDOMNode = oXMLDOMNamedNodeMap.getNamedItem(name) Parameters name String specifying the name of the attribute. Return Value Object. Returns IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.Example Dim xmlDoc As New Msxml2.DOMDocument Dim nodeBook As IXMLDOMElement Dim nodeId As IXMLDOMAttribute xmlDoc.async = False xmlDoc.Load "books.xml" Set nodeBook = xmlDoc.selectSingleNode("//book") Set nodeId = nodeBook.Attributes.getNamedItem("id") MsgBox nodeId.Value [C/C++] C/C++ Syntax HRESULT getNamedItem( BSTR name, IXMLDOMNode **namedItem); Parameters name [in] Name of the attribute. namedItem [out, retval] IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection. C/C++ Return Values S_OK Value returned if successful. S_FALSE Value when returning Null. E_INVALIDARG Value returned if namedItem is Null. C/C++ Example IXMLDOMNode *pIXMLDOMNode = NULL; IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL; BSTR bstrAttributeName = ::SysAllocString(_T("dateModified")); IXMLDOMElement *pIXMLDOMElement = NULL; IXMLDOMDocument *pIXMLDOMDocument = NULL; VARIANT varValue;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) { hr = pIXMLDOMElement->get_attributes(&pIXMLDOMNamedNodeMap); if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap) { hr = pIXMLDOMNamedNodeMap->getNamedItem(bstrAttributeName, &pIXMLDOMNode); if(SUCCEEDED(hr) && pIXMLDOMNode) { pIXMLDOMNode->get_nodeValue(&varValue); ::MessageBox(NULL, _bstr_t(varValue), _T("Item Value"), MB_OK); pIXMLDOMNode->Release(); pIXMLDOMNode = NULL; } pIXMLDOMNamedNodeMap->Release(); pIXMLDOMNamedNodeMap = NULL; } pIXMLDOMElement->Release(); pIXMLDOMElement = NULL; } ::SysFreeString(bstrAttributeName); bstrAttributeName = NULL; } catch(...) { if(bstrAttributeName) ::SysFreeString(bstrAttributeName); if(pIXMLDOMElement) pIXMLDOMElement->Release(); if(pIXMLDOMNamedNodeMap) pIXMLDOMNamedNodeMap->Release(); if(pIXMLDOMNode) pIXMLDOMNode->Release(); DisplayErrorToUser(); } // Release pIXMLDOMDocument when finished with it.
C/C++ Syntax HRESULT getNamedItem( BSTR name, IXMLDOMNode **namedItem); Parameters name [in] Name of the attribute. namedItem [out, retval] IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection. C/C++ Return Values S_OK Value returned if successful. S_FALSE Value when returning Null. E_INVALIDARG Value returned if namedItem is Null.
Retrieves the attribute with the specified name.[Script]
Script Syntax
var objXMLDOMNode = oXMLDOMNamedNodeMap.getNamedItem(name);
Parameters
name
String specifying the name of the attribute.
Return Value
Object. Returns IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.Example
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
var nodeBook, nodeId;
xmlDoc.async = false;
xmlDoc.load("books.xml");
nodeBook = xmlDoc.selectSingleNode("//book");
nodeId = nodeBook.attributes.getNamedItem("id");
alert(nodeId.value);
[Visual Basic]
Visual Basic Syntax
Set objXMLDOMNode = oXMLDOMNamedNodeMap.getNamedItem(name)
Parameters
name
String specifying the name of the attribute.
Return Value
Object. Returns IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.Example
Dim xmlDoc As New Msxml2.DOMDocument
Dim nodeBook As IXMLDOMElement
Dim nodeId As IXMLDOMAttribute
xmlDoc.async = False
xmlDoc.Load "books.xml"
Set nodeBook = xmlDoc.selectSingleNode("//book")
Set nodeId = nodeBook.Attributes.getNamedItem("id")
MsgBox nodeId.Value
[C/C++]
C/C++ Syntax
HRESULT getNamedItem(
BSTR name,
IXMLDOMNode **namedItem);
Parameters
name [in]
Name of the attribute.
namedItem [out, retval]
IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.
C/C++ Return Values
S_OK
Value returned if successful.
S_FALSE
Value when returning Null.
E_INVALIDARG
Value returned if namedItem is Null.
C/C++ Example
IXMLDOMNode *pIXMLDOMNode = NULL;
IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL;
BSTR bstrAttributeName = ::SysAllocString(_T("dateModified"));
IXMLDOMElement *pIXMLDOMElement = NULL;
IXMLDOMDocument *pIXMLDOMDocument = NULL;
VARIANT varValue;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)
{
hr = pIXMLDOMElement->get_attributes(&pIXMLDOMNamedNodeMap);
if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap)
{
hr = pIXMLDOMNamedNodeMap->getNamedItem(bstrAttributeName, &pIXMLDOMNode);
if(SUCCEEDED(hr) && pIXMLDOMNode)
{
pIXMLDOMNode->get_nodeValue(&varValue);
::MessageBox(NULL, _bstr_t(varValue), _T("Item Value"), MB_OK);
pIXMLDOMNode->Release();
pIXMLDOMNode = NULL;
}
pIXMLDOMNamedNodeMap->Release();
pIXMLDOMNamedNodeMap = NULL;
}
pIXMLDOMElement->Release();
pIXMLDOMElement = NULL;
}
::SysFreeString(bstrAttributeName);
bstrAttributeName = NULL;
}
catch(...)
{
if(bstrAttributeName)
::SysFreeString(bstrAttributeName);
if(pIXMLDOMElement)
pIXMLDOMElement->Release();
if(pIXMLDOMNamedNodeMap)
pIXMLDOMNamedNodeMap->Release();
if(pIXMLDOMNode)
pIXMLDOMNode->Release();
DisplayErrorToUser();
}
// Release pIXMLDOMDocument when finished with it.
HRESULT getNamedItem(
BSTR name,
IXMLDOMNode **namedItem);
Parameters
name [in]
Name of the attribute.
namedItem [out, retval]
IXMLDOMNode object for the specified attribute. Returns Null if the attribute node is not in this collection.
C/C++ Return Values
S_OK
Value returned if successful.
S_FALSE
Value when returning Null.
E_INVALIDARG
Value returned if namedItem is Null.
nodeId = nodeBook.attributes.getNamedItem("id");
为什么传一个参数也可以?第二个参数是默认参数?
Visual Basic Syntax
[C/C++]
C/C++ Syntax
这些红色的地方说明了