Creating an xml file from java.

I trying to create an xml file using a java program. I just wondering what is the best way to go about it and what should i use jdom ,xerces sax etc.
[156 byte] By [djSpeefa] at [2007-11-27 6:43:51]
# 1

Use JAXP+SAX.

Here an example:import java.io.*;

// SAX classes.

import org.xml.sax.*;

import org.xml.sax.helpers.*;

//JAXP 1.1

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.stream.*;

import javax.xml.transform.sax.*;

[...]

// PrintWriter from a Servlet

PrintWriter out = response.getWriter();

StreamResult streamResult = new StreamResult(out);

SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

// SAX2.0 ContentHandler.

TransformerHandler hd = tf.newTransformerHandler();

Transformer serializer = hd.getTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");

serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");

serializer.setOutputProperty(OutputKeys.INDENT,"yes");

hd.setResult(streamResult);

hd.startDocument();

AttributesImpl atts = new AttributesImpl();

// USERS tag.

hd.startElement("","","USERS",atts);

// USER tags.

String[] id = {"PWD122","MX787","A4Q45"};

String[] type = {"customer","manager","employee"};

String[] desc = {"Tim@Home","Jack&Moud","John D'o?};

for (int i=0;i<id.length;i++)

{

atts.clear();

atts.addAttribute("","","ID","CDATA",id[i]);

atts.addAttribute("","","TYPE","CDATA",type[i]);

hd.startElement("","","USER",atts);

hd.characters(desc[i].toCharArray(),0,desc[i].length());

hd.endElement("","","USER");

}

hd.endElement("","","USERS");

hd.endDocument();

[...]

This xml generation program might be the best solution because it uses JAXP 1.1 so it will work under JDK 1.4 or JDK 1.2/1.3 with XALAN2 library (or any XML library JAXP 1.1 compliant). It's also memory-friendly because it doesn't need DOM.

See http://www.javazoom.net/services/newsletter/xmlgeneration.html

Hope That Helps>

java_2006a at 2007-7-12 18:14:51 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hello

> I trying to create an xml file using a java program.

> I just wondering what is the best way to go about it

> and what should i use jdom ,xerces sax etc.

Take a look at the URL, before deciding which one to use. I use DOM parser.

http://java.sun.com/webservices/docs/1.6/tutorial/doc/SJSXP2.html#wp103460

haishaia at 2007-7-12 18:14:51 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Here is the pgm

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.TransformerFactoryConfigurationError;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

public class XMLCreator {

public static void main(String[] args) {

try {

(new XMLCreator()).create();

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (TransformerFactoryConfigurationError e) {

e.printStackTrace();

} catch (TransformerException e) {

e.printStackTrace();

}

}

private void create() throws ParserConfigurationException,

TransformerFactoryConfigurationError, TransformerException {

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document document = builder.newDocument();

Element rootElement = document.createElement("root");

document.appendChild(rootElement);

Element childElement = document.createElement("child");

rootElement.appendChild(childElement);

System.out.println(document.toString());

System.out.println(rootElement);

saveDocument(document);

}

private void saveDocument(Document document)

throws TransformerFactoryConfigurationError, TransformerException {

Transformer transformer = TransformerFactory.newInstance().newTransformer();

DOMSource xmlSource = new DOMSource(document);

StreamResult result = new StreamResult(System.out);

transformer.transform(xmlSource , result );

}

}

haishaia at 2007-7-12 18:14:51 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I decided to go with this program and i changed it around to suit my needs. So now i am able to create an xml file. I just have one question i want to add a stylesheet to xml file so i need to put a line after the <?xml version="1.0" encoding="UTF-8"?> . the line that links the stylesheet to the xml file

<?xml-stylesheet type="text/xsl". How do i add this line.

djSpeefa at 2007-7-12 18:14:51 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...