In Delphi XE is it possible to use XPath with a TXmlDocument
component?
I'm aware I can use late binding to access the MSXML2 and then use XPath:
XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');
But I wanna know if TXmlDocument
installed with Delphi XE supports XPath.
Instances of the XPathDocument class are created using one of its six constructors. These constructors allow you to read an XML document using a Stream, TextReader, or XmlReader object, as well as the string path to an XML file. The following example illustrates using the XPathDocument class's string constructor to read an XML document.
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.
The XPathDocument class is read-only while the XmlDocument class is editable and as a result, XPathNavigator objects created from an XPathDocument object cannot be used to edit an XML document while those created from an XmlDocument object can. The XPathDocument class should be used to read an XML document only.
XPath can be used to navigate through elements and attributes in an XML document. XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT and in XQuery XPath is a W3C recommendation
I can't find anything in the TXMLDocument documentation about XPath.
XML example, from the OmniXML XPath demo:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
</book>
<book>
<title lang="eng">Learning XML</title>
</book>
<book>
<title lang="slo">Z OmniXML v lepso prihodnost</title>
<year>2006</year>
</book>
<book>
<title>Kwe sona standwa sam</title>
</book>
</bookstore>
Try something like this:
uses
XMLDoc, XMLDom, XMLIntf;
// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
var
IDoc: IXMLDocument;
INode: IXMLNode;
begin
IDoc := LoadXMLDocument('.\books.xml');
INode := SelectNode(IDoc.DocumentElement,'/bookstore/book[2]/title');
end;
Just as an FYI for others, I'll leave this in: OmniXML supports XPath, and has a demo that shows really well how to use it. It's also free, comes with source, supports Unicode, and has pretty good support through it's forums.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With