IXMLDOMSelection
Represents the list of nodes that match a given XML Path Language (XPath) expression.
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.DOMDocument5.0"); xmlDoc.setProperty("SelectionLanguage", "XPath"); var selection = xmlDoc.selectNodes("//book"); var expr = selection.expr; //This returns "//book" alert(expr);
Example
In the following Microsoft Visual Basic® example, the selectNodes
method returns IXMLDOMNodeList
, so you have to cast the result of selectNodes
into an IXMLDOMSelection
.
Dim xmlDoc As New Msxml2.DOMDocument50 Dim Selection As IXMLDOMSelection Dim sExpr As String xmlDoc.async = False xmlDoc.setProperty "SelectionLanguage", "XPath" xmlDoc.Load ("books.xml") If (xmlDoc.parseError.errorCode <> 0) Then Dim myErr Set myErr = xmlDoc.parseError MsgBox("You have error " & myErr.reason) Else Set Selection = xmlDoc.selectNodes("//book") sExpr = Selection.expr ' This returns "//book". MsgBox (sExpr) End If
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
.
#import "msxml5.dll" using namespace MSXML2; #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("d:\\inetpub\\wwwroot\\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; }
d:\\inetpub\\wwwroot\\samplexmldtd.xml
<?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 (in a message box)
Lover Birds Cynthia Randall Lucerne Publishing
Remarks
IXMLDOMSelection
is an extension of the World Wide Web Consortium (W3C) DOM.
Versioning
MSXML 2.6 and later
Requirements
Implementation: msxml5.dll, msxml2.lib
Header and IDL files: msxml2.h, msxml2.idl
Version-Dependent ProgID: Msxml2.DOMDocument.5.0, Msxml2.FreeThreadedDOMDocument.5.0
Version-Dependent CLSID: 88d969c0-f192-11d4-a65f-0040963251e5
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.