• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
  • Frits Walraven
  • fred rosenberger
  • salvin francis

creating a new xml document

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
// Data to be loaded into the XML doc
String senders [] = {"sender1@dot.com",
String receivers [] = {"rec1@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");
// 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");
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) {
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic