<s1 title="Adding Elements">

<p>Elements in XML are the same as tags.  This example will create a
document and add some elements to it.</p>

<example title="Resulting XML">
&lt;top>
  &lt;a/>
  &lt;b/>
  &lt;c/>
&lt;/top>
</example>

<p>All the DOM nodes are created using the Document object.  Each node
created by a Document belongs to that document.  You can't directly add
nodes from one document to another document.  (Although you can <var/import/>
the node.)</p>

<p>Documents are intrinsically implementation-dependent.
To bootstrap this process, you need to create the Document.
Sun's JAXP DocumentBuilder gives an implementation-independent method
for creating a document.</p>

<example title="Creating Elements with the DOM">
import java.io.*;
import javax.xml.parser.*;
import org.w3c.dom.*;
import com.caucho.xml.*;

...

// Create a new parser using the JAXP API (javax.xml.parser)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

// Create a new document
Document doc = builder.newDocument();

// Create the top element
Element top = doc.createElement("top");

// Add it to the document
doc.appendChild(top);

// Add elements to the top
top.appendChild(doc.createElement("a"));
top.appendChild(doc.createElement("b"));
top.appendChild(doc.createElement("c"));
</example>

</s1>