File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "creating a new xml document" Watch "creating a new xml document" New topic
Author

creating a new xml document

andy yeo
Greenhorn

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.*;
import java.io.*;
public class BuildJdom {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java BuildJdom URL");
System.exit(0);
}
//
// Data to be loaded into the XML doc
String senders [] = {"sender1@dot.com",
"sender2@dot.com"};
String receivers [] = {"rec1@dot.com",
"rec2@dot.com"};
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");
mailVector.add(messageTag);
}
rootElement.setChildren(mailVector);
//
// Load nodes under Message nodes
//
int i = 0;
Iterator it = mailVector.iterator();
while (it.hasNext()) {
Element el = (Element)it.next();
Vector messageVector = new Vector();
if (i < senders.length) {
fromTag = new Element("From");
toTag = new Element("To");
textTag = new Element("Text");
fromTag.setText(senders[i]);
toTag.setText(receivers[i]);
textTag.setText(messages[i]);
messageVector.add(fromTag);
messageVector.add(toTag);
messageVector.add(textTag);
el.setChildren(messageVector);
i++;
}
}
//
//
Document doc = new Document(rootElement,
new DocType("Mail", "mail.dtd"));
//
try {
String indent = " ";
boolean newLines= true;
XMLOutputter pr = new XMLOutputter(indent,
newLines);
pr.output(doc,new FileOutputStream(args[0] + ".xml"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a new xml document
 
Similar Threads
how to find content length of the xml document
Converting Document Object to String
how to copy elements from one doc to other ?
XML and DTD
writing xml document using xml schema using Eclipse