Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

The output shown is not precisely what is generated by this example, whitespace was added to make it easier to read.

To build a Document Object Model, we use JAXP's DocumentBuilder.

After instance fabrication by its factory...

we request it make an org.w3c.dom.Document.

Create an org.w3c.dom.Element...

and start building the DOM.

After creating somer text-nodes and elements,...

we attach aliment3 as child to the root element...

and add text-node 'bee' to it.

We then insert elm2 before elm3...

and append text-node 'ay' to it.

Add an attribute to aliment2.

elm3 is now the root element's last child and elm5 is attached to it.

This makes elm3's children elm5 and elm4, in that order.

Now try to figure this out. Siblings are nodes that have the same parent.

Adding some comment.

Use javax.xml.transform.dom.DOMSource, 'src'...

and these, 'out',...

to have a Transformer write the XML file to the current directory (as indicated by the system property 'user.dir').