The moose likes XML and Related Technologies and the fly likes creating a new xml document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "creating a new xml document" Watch "creating a new xml document" New topic

creating a new xml document

andy yeo

Joined: Jul 26, 2001
Posts: 3
I am a little confused here. All the examples codes I have found on the web using dom and jaxp is about converting with a existing xml document. Is there any way to create a new xml document?
Ambrose Tati
Ranch Hand

Joined: Oct 03, 2000
Posts: 55
Hi Andy,
Of course you can create a new xml document rather than just manipulate an existing xml document. Look at the DOM API and you'll see how to use the different methods. I found it much easier to work with JDOM, you can visit the JDOM home page and download the relevant jar files.
I've put together the following small program which uses jdom to create a small xml file:
// JDOM classes
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
// Standard classes
import java.util.*;
public class BuildJdom {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java BuildJdom URL");
// Data to be loaded into the XML doc
String senders [] = {"",
String receivers [] = {"",
String messages [] = {"message1","message2"};
Element rootElement = null;
Element messageTag = null;
Element fromTag = null;
Element toTag = null;
Element textTag = null;
rootElement = new Element("Mail");
Vector mailVector = new Vector();
for (int i = 0; i < senders.length; i++) {
messageTag = new Element("Message");
// Load nodes under Message nodes
int i = 0;
Iterator it = mailVector.iterator();
while (it.hasNext()) {
Element el = (Element);
Vector messageVector = new Vector();
if (i < senders.length) {
fromTag = new Element("From");
toTag = new Element("To");
textTag = new Element("Text");
Document doc = new Document(rootElement,
new DocType("Mail", "mail.dtd"));
try {
String indent = " ";
boolean newLines= true;
XMLOutputter pr = new XMLOutputter(indent,
pr.output(doc,new FileOutputStream(args[0] + ".xml"));
} catch (Exception e) {
I agree. Here's the link:
subject: creating a new xml document
It's not a secret anymore!