• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating a new xml document

 
Greenhorn
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");
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());
}
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic