Elements in XML are the same as tags. This example will create a
document and add some elements to it.
Resulting XML
<top>
<a/>
<b/>
<c/>
</top>
|
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 import
the node.)
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.
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"));
|
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|