|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE task |
|
11 PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd"> |
|
12 <task id="GUID-92C2317A-57B8-558E-984B-B10FD0404981" xml:lang="en"><title>Searching |
|
13 a DOM Tree using XPath</title><shortdesc>This topic explains how to search in a DOM tree using an XPath |
|
14 expression. </shortdesc><prolog><metadata><keywords/></metadata></prolog><taskbody> |
|
15 <prereq><p>Before you start, you must: </p> <ul> |
|
16 <li id="GUID-3B777E07-07AE-5EF0-8CB1-F07217AFB2C2"><p>understand <xref href="http://www.w3schools.com/XPath/xpath_syntax.asp" scope="external">XPath syntax</xref> and the concept of <xref href="http://www.w3.org/DOM/" scope="external">Document Object Model (DOM)</xref>. </p> </li> |
|
17 <li id="GUID-ADB75A14-811C-53F9-90C3-B068D1506EE1"><p>understand the architecture |
|
18 and classes of the <xref href="GUID-5ACFA8E4-3C4B-5851-A43E-9FA72D0B8403.dita">XML |
|
19 Engine</xref> component. </p> </li> |
|
20 <li id="GUID-1AF969E2-E11F-538B-A33C-762B1F0E5EA6"><p>have created an <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>RXmlEngDocument</apiname></xref> (called <codeph>myDoc</codeph> in this |
|
21 example), either by parsing an XML file (as indicated in the <xref href="GUID-C5C30218-311C-58D9-A990-3F95642F7517.dita">XML |
|
22 DOM Parsing Tutorial</xref>) or by adding nodes to a new DOM tree. </p> </li> |
|
23 </ul> </prereq> |
|
24 <context><p>XPath is a simple query language for XML. An XPath expression |
|
25 describes a subset of an XML tree, such as all the elements with a specified |
|
26 name, or all the children of a specific node. Evaluating an XPath expression |
|
27 means searching in the XML tree for matching nodes or content: clients of |
|
28 the XML Engine use it to explore XML data. </p> <p>The following steps show |
|
29 how to make an XPath query with the XML Engine. </p> </context> |
|
30 <steps id="GUID-AE83D04E-2094-5F9C-AD56-D9803C96FB2C"> |
|
31 <step id="GUID-3E1709B9-A282-533F-B893-3E4962B29AA4"><cmd>Create an instance |
|
32 of <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>RXmlEngXPathExpression</apiname></xref> by calling the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngXpathEvaluator::CreateExpressionL()</apiname></xref> method. </cmd> |
|
33 <stepxmp><codeblock id="GUID-95ED0151-D8E7-5CB2-9F3A-90C691B253B1" xml:space="preserve">RXmlEngXPathExpression expr = TXmlEngXpathEvaluator::CreateExpressionL( "@*" );</codeblock> </stepxmp> |
|
34 <info>This XPath expression is only useful to illustrate this tutorial: it |
|
35 returns all the attributes in an XML tree. </info> |
|
36 </step> |
|
37 <step id="GUID-77F768C2-E2F8-55AF-AAC3-DF95347AFBD2"><cmd/> |
|
38 <info>Evaluate the XPath expression by applying it to your XML document. </info> |
|
39 <stepxmp><codeblock id="GUID-65F9578E-CF76-5CCA-A8F5-D73E32C7D9D8" xml:space="preserve">RXmlEngXPathResult searchResult = expr.EvaluateL( myDoc );</codeblock> </stepxmp> |
|
40 <stepresult>The <codeph>searchResult</codeph> variable contains a tree of <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngAttr</apiname></xref> objects. This tree is a subset of the DOM |
|
41 tree. </stepresult> |
|
42 </step> |
|
43 <step id="GUID-C9981839-7BDB-5448-B161-A3524DD79AC9"><cmd/> |
|
44 <info>Use the result, for example by accessing it as a node set as follows: </info> |
|
45 <stepxmp><codeblock id="GUID-ABFD2D88-C8A0-58FE-9408-05B3F2714794" xml:space="preserve">RXMLEngNodeSet set = result.AsNodeSet(); |
|
46 if( for TInt i = 0; i < set.Length(); i++ ) |
|
47 { |
|
48 TXmlEngNode resultNode = set[i]; |
|
49 /* [...] do something with the result */ |
|
50 }</codeblock> </stepxmp> |
|
51 </step> |
|
52 <step id="GUID-0D55DFAA-B583-5582-B197-8D07DEF37636"><cmd/> |
|
53 <info>Free your resources as necessary. </info> |
|
54 </step> |
|
55 </steps> |
|
56 </taskbody><related-links> |
|
57 <link href="GUID-E20C2C98-A294-551B-99CF-BE1DAA6F597E.dita"><linktext>Serialising |
|
58 XML DOM</linktext></link> |
|
59 </related-links></task> |