IXMLDOMSelection
Represents the list of nodes that match a given XML Path Language (XPath) expression.
JScript Example
In the following Microsoft® JScript® example, you can simply call the IXMLDOMSelection
methods on the object returned from selectNodes
.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
xmlDoc.async = false;
xmlDoc.setProperty("SelectionLanguage", "XPath");
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
var selection = xmlDoc.selectNodes("//book");
var expr = selection.expr; //This returns "//book"
WScript.Echo(expr);
}
Output
//book
C/C++ Example
In the following C/C++ example, IXMLDOMSelection
inherits the threading model of the document that created it. IXMLDOMSelection
is created through the selectNodes
method on IXMLDOMDocument2
.
#include “msxml6.h”
#define CHECK_AND_RELEASE(pInterface) \
if(pInterface) \
{\
pInterface->Release();\
pInterface = NULL;\
}\
#define RELEASE(pInterface) \
{\
pInterface->Release();\
pInterface = NULL;\
}\
BOOL DOMSelectionDemo()
{
BOOL bResult = FALSE;
short sResult = FALSE;
IXMLDOMSelection *pIXMLDOMSelection=NULL;
IXMLDOMNodeList *pIXMLDOMNodeList=NULL;
IXMLDOMNode *pIXMLDOMNode=NULL;
IXMLDOMDocument2 *pIXMLDOMDocument2=NULL;
BSTR bstrValue;
HRESULT hr;
try
{
hr=CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (LPVOID*)(&pIXMLDOMDocument2));
SUCCEEDED(hr) ? 0 : throw hr;
if(pIXMLDOMDocument2)
{
hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE);
if(SUCCEEDED(hr))
{
hr=pIXMLDOMDocument2->load(_variant_t(
_T("samplexmldtd.xml")), &sResult);
if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
{
hr=pIXMLDOMDocument2->selectNodes(
_T("*/BOOK[TITLE='Lover Birds']"
), &pIXMLDOMNodeList);
if(SUCCEEDED(hr))
{
hr=pIXMLDOMNodeList->QueryInterface(IID_IXMLDOMSelection
,(void**)&pIXMLDOMSelection );
if(SUCCEEDED(hr) && pIXMLDOMSelection)
{
LONG uLength;
bResult=TRUE;
hr=pIXMLDOMSelection->get_length(&uLength);
if(SUCCEEDED(hr))
{
for(int iIndex=0; iIndex < uLength; iIndex++)
{
// remove all the nodes from the list-display
// them as they are removed.
hr=pIXMLDOMSelection->removeNext(
&pIXMLDOMNode);
if(SUCCEEDED(hr) && pIXMLDOMNode)
{
hr=pIXMLDOMNode->get_text(&bstrValue);
if(SUCCEEDED(hr))
::MessageBox(NULL, bstrValue, _T("Node
Text"), MB_OK);
RELEASE(pIXMLDOMNode);
}
}
}
RELEASE(pIXMLDOMSelection);
}
RELEASE(pIXMLDOMNodeList);
}
}
}
RELEASE(pIXMLDOMDocument2);
}
}
catch(...)
{
CHECK_AND_RELEASE(pIXMLDOMNode);
CHECK_AND_RELEASE(pIXMLDOMDocument2);
CHECK_AND_RELEASE(pIXMLDOMNodeList);
CHECK_AND_RELEASE(pIXMLDOMSelection);
DisplayErrorToUser();
}
return bResult;
}
Samplexmldtd.xml
The C++ example uses the following XML file.
<?xml version='1.0'?>
<COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes">
<DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
<BOOK>
<TITLE>Lover Birds</TITLE>
<AUTHOR>Cynthia Randall</AUTHOR>
<PUBLISHER>Lucerne Publishing</PUBLISHER>
</BOOK>
<BOOK>
<TITLE>The Sundered Grail</TITLE>
<AUTHOR>Eva Corets</AUTHOR>
<PUBLISHER>Lucerne Publishing</PUBLISHER>
</BOOK>
<BOOK>
<TITLE>Splish Splash</TITLE>
<AUTHOR>Paula Thurman</AUTHOR>
<PUBLISHER>Scootney</PUBLISHER>
</BOOK>
</COLLECTION>
Output
The example outputs the following in a message box.
Lover Birds Cynthia Randall Lucerne Publishing
Remarks
IXMLDOMSelection
is an extension of the World Wide Web Consortium (W3C) DOM.
Requirements
Implementation:
msxml3.dll, msxml2.lib (MSXML 3.0)
msxml6.dll, msxml6.lib (MSXML 6.0)
Header and IDL files:
msxml2.h, msxml2.idl, msxml6.h, msxml6.idl
Version-Dependent ProgID:
Msxml2.DOMDocument.5.0, Msxml2.FreeThreadedDOMDocument.5.0
Version-Dependent CLSID:
88d969c0-f192-11d4-a65f-0040963251e5
Versioning
Implemented in:
MSXML 3.0, MSXML 6.0