Modifying DOM Tree

This topic explains how to create or modify a DOM tree by adding nodes and changing their contents.

Context

The following diagram shows a sequence of calls that create a DOM tree from an empty document. There are many functions in the DOM Engine API that add nodes to a tree: refer to the reference documentation of the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngElement class for a comprehensive list.

Figure: Creating a DOM tree

Prerequisites

Before you start, you must:

Steps

  1. Create an instance of [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]RXmlEngDocument.
    RXmlEngDocument myDoc;
  2. Set the root node with the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]RXmlEngDocument::CreateDocumentElementL() function.
    _LIT8( KRootName, "playlist" );
    TXmlEngElement root = myDoc->CreateDocumentElementL( KRootName );
  3. Add child nodes and attributes by calling the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngElement::AddNewElementL() and [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngElement::AddNewAttributeL() functions.
    _LIT8( KChildName, "song" );
    _LIT8( KAttrName, "filename" );
    
    for( TInt i=1;i<6;i++ ) 
        {
              TXmlEngElement tempElement = root.AddNewElementL( KChildName );
              TBuf8<12> value( _L8( "music0" ) );
              value.AppendNum( i );
              value.Append( _L8( ".mp3" ) );
              tempElement.AddNewAttributeL( KAttrName, value );
        }
  4. Swap the first two song elements by calling the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngElement::MoveAfterSibling() function.
    TXmlEngNode swap = root.FirstChild();
    swap.MoveAfterSibling( swap.NextSibling() );

Results

The result is a DOM tree corresponding to the following XML structure:

 <playlist>
     <song filename = "music02.mp3"/>
     <song filename = "music01.mp3"/>
     <song filename = "music03.mp3"/>
     <song filename = "music04.mp3"/>
     <song filename = "music05.mp3"/>
 </playlist>