Modifying DOM Tree

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


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 TXmlEngElement class for a comprehensive list.

Figure: Creating a DOM tree


Before you start, you must:


  1. Create an instance of RXmlEngDocument.


    RXmlEngDocument myDoc;
  2. Set the root node with the RXmlEngDocument::CreateDocumentElementL() function.


    _LIT8( KRootName, "playlist" );
    TXmlEngElement root = myDoc->CreateDocumentElementL( KRootName );
  3. Add child nodes and attributes by calling the TXmlEngElement::AddNewElementL() and 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 TXmlEngElement::MoveAfterSibling() function.


    TXmlEngNode swap = root.FirstChild();
    swap.MoveAfterSibling( swap.NextSibling() );


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

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