|
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-61056CE2-97BF-5D32-8AC5-E625FBC7D0EC" xml:lang="en"><title>Modifying |
|
13 DOM Tree</title><shortdesc>This topic explains how to create or modify a DOM tree by adding |
|
14 nodes and changing their contents. </shortdesc><prolog><metadata><keywords/></metadata></prolog><taskbody> |
|
15 <prereq><p>Before you start, you must: </p> <ul> |
|
16 <li id="GUID-AB0E481B-349C-5C60-ACB7-06EBA299E149"><p>understand the concept |
|
17 of <xref href="http://www.w3.org/DOM/" scope="external">Document Object Model |
|
18 (DOM)</xref> </p> </li> |
|
19 <li id="GUID-79A66EA2-4F81-57E9-A4DB-E64B5405B578"><p>understand the structure |
|
20 and classes of the <xref href="GUID-5ACFA8E4-3C4B-5851-A43E-9FA72D0B8403.dita">XML |
|
21 DOM Engine </xref> component </p> </li> |
|
22 </ul> </prereq> |
|
23 <context><p> </p> <p>The following diagram shows a sequence of calls that |
|
24 create a DOM tree from an empty document. There are many functions in the |
|
25 DOM Engine API that add nodes to a tree: refer to the reference documentation |
|
26 of the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngElement</apiname></xref> class for a comprehensive |
|
27 list. </p> <fig id="GUID-31E4FB65-7C28-513B-B8C0-5B057DC64A50"> |
|
28 <title> Creating a DOM tree </title> |
|
29 <image href="GUID-3E0E2E54-CD8A-5BAF-A87B-BF51FD8890D6_d0e671390_href.png" placement="inline"/> |
|
30 </fig> </context> |
|
31 <steps id="GUID-DEA52036-E11C-5913-984B-663DD68D02DB"> |
|
32 <step id="GUID-E65A2FF6-2918-540B-97DB-C07500A6CB08"><cmd/> |
|
33 <info>Create an instance of <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>RXmlEngDocument</apiname></xref>. </info> |
|
34 <stepxmp><codeblock id="GUID-B36F5C00-6974-54EC-939A-34BF05F5C04A" xml:space="preserve">RXmlEngDocument myDoc;</codeblock> </stepxmp> |
|
35 </step> |
|
36 <step id="GUID-858AD52F-0526-5DCC-B424-666D88F19E8E"><cmd/> |
|
37 <info>Set the root node with the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>RXmlEngDocument::CreateDocumentElementL()</apiname></xref> function. </info> |
|
38 <stepxmp><codeblock id="GUID-E890E160-294C-5218-B8A8-41EB7E7FE8A7" xml:space="preserve">_LIT8( KRootName, "playlist" ); |
|
39 TXmlEngElement root = myDoc->CreateDocumentElementL( KRootName );</codeblock> </stepxmp> |
|
40 </step> |
|
41 <step id="GUID-25D56F38-012F-5169-9925-07A6A7BBAC31"><cmd/> |
|
42 <info>Add child nodes and attributes by calling the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngElement::AddNewElementL()</apiname></xref> and <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngElement::AddNewAttributeL()</apiname></xref> functions. </info> |
|
43 <stepxmp><codeblock id="GUID-5CCB071C-7042-5F07-A5DE-29C2D5CD01D6" xml:space="preserve">_LIT8( KChildName, "song" ); |
|
44 _LIT8( KAttrName, "filename" ); |
|
45 |
|
46 for( TInt i=1;i<6;i++ ) |
|
47 { |
|
48 TXmlEngElement tempElement = root.AddNewElementL( KChildName ); |
|
49 TBuf8<12> value( _L8( "music0" ) ); |
|
50 value.AppendNum( i ); |
|
51 value.Append( _L8( ".mp3" ) ); |
|
52 tempElement.AddNewAttributeL( KAttrName, value ); |
|
53 }</codeblock> </stepxmp> |
|
54 </step> |
|
55 <step id="GUID-559B34A9-4A69-57E3-AF2A-D3E7F81728B0"><cmd/> |
|
56 <info>Swap the first two song elements by calling the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngElement::MoveAfterSibling()</apiname></xref> function. </info> |
|
57 <stepxmp><codeblock id="GUID-6B4EDCE6-A688-5A0E-B4D9-9CF2ABD8DE5F" xml:space="preserve">TXmlEngNode swap = root.FirstChild(); |
|
58 swap.MoveAfterSibling( swap.NextSibling() );</codeblock> </stepxmp> |
|
59 </step> |
|
60 <step id="GUID-D648ADF3-10A5-5FDC-BF2C-D08B095E5FE6"><cmd/> |
|
61 <info>If you want to save the DOM tree into a file, call the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>XmlEnginePushL()</apiname></xref> method |
|
62 first. </info> |
|
63 <info>You must call the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>XmlEnginePushL()</apiname></xref> method |
|
64 before parsing or saving an XML file, and the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>XmlEnginePopAndClose()</apiname></xref> method |
|
65 after these operations. </info> |
|
66 </step> |
|
67 </steps> |
|
68 <result><p>The result is a DOM tree corresponding to the following XML structure: </p> <codeblock id="GUID-E90DD401-DDA6-5A3F-9A26-8C5CBE6BE64E" xml:space="preserve"> |
|
69 <playlist> |
|
70 <song filename = "music02.mp3"/> |
|
71 <song filename = "music01.mp3"/> |
|
72 <song filename = "music03.mp3"/> |
|
73 <song filename = "music04.mp3"/> |
|
74 <song filename = "music05.mp3"/> |
|
75 </playlist> |
|
76 </codeblock> </result> |
|
77 </taskbody><related-links> |
|
78 <link href="GUID-C5C30218-311C-58D9-A990-3F95642F7517.dita"><linktext>Parsing XML |
|
79 DOM</linktext></link> |
|
80 </related-links></task> |