A Branch (a Document or Element) has a method called content() which returns
a modifiable List. So you can use the standard Java 2 List interface to
change the content of an Element or Document.

e.g.

DocumentFactory factory = new DocumentFactory();
Document doc = factory.createDocument();
Element root = doc.addElement( "html" );
Element header = root.addElement( "header" );
Element footer = root.addElement( "footer" );

// now lets add <foo> in between header & footer
List list = root.content();
Element foo = factory.createElement( "foo" );
list.add( 1, foo );

// assertions
assertTrue( list.size() == 3 );
assertTrue( list.get(0) == header );
assertTrue( list.get(1) == foo );
assertTrue( list.get(2) == footer );


The above code has all been added to the unit test
dom4j/src/test/org/dom4j/TestContent in the method testAddingInTheMiddle().

James
----- Original Message -----
From: "Christian Holmqvist, IT, Posten" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, April 23, 2002 2:32 PM
Subject: [dom4j-user] Add element between elements in a document.


> Hi
>
> I have a small problem with adding elements.
>
> I got a document that looks something like this:
>
> Doc
>  |
>  --- Header
>  |
>  |
>  --- Footer
>
> On the document handler class (my class) there is a method called add
> element. This element should be placed in between the header and the
footer
> i.e:
>
> Doc
>  |
>  --- Header
>  |
>  -> (new element)
>  |
>  --- Footer
>
> My problem is that I can not figure out how to add a elemnt to a specific
> position in a document.
>
> Help... please...
>
> Cheers Christian Holmqvist
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to