Best Practices for Writing XML

R

Rhino

What are the best-regarded Java classes for writing XML these days?

I'm afraid I've been out of touch with XML for a few years and long ago lost
track of what is well-regarded in the Java community for creating XML files.

I have a need to write an XML file but NOT to work with it any further; the
file will be read by Microsoft Word but not processed any further. If
someone can point me to some suitable classes for writing XML - and,
ideally, a tutorial describing the proper use of these classes - I would
really appreciate that.
 
T

Tom Anderson

What are the best-regarded Java classes for writing XML these days?

I'm afraid I've been out of touch with XML for a few years and long ago
lost track of what is well-regarded in the Java community for creating
XML files.

I have a need to write an XML file but NOT to work with it any further;
the file will be read by Microsoft Word but not processed any further.
If someone can point me to some suitable classes for writing XML - and,
ideally, a tutorial describing the proper use of these classes - I would
really appreciate that.

I'd probably go with StAX for writing it. It's in the standard library as
of 1.6, and there's a backport to 1.4+. It's pretty straightforward, and
there javadoc is easy to find, so i'll lame out of supplying links.

There aren't really any other standardised XML-writing libraries; i think
you'd have to make a DOM tree and feed it to some kind of serialiser via
JAXP or something, which you don't want to do.

tom
 
D

Daniel Pitts

I'd probably go with StAX for writing it. It's in the standard library
as of 1.6, and there's a backport to 1.4+. It's pretty straightforward,
and there javadoc is easy to find, so i'll lame out of supplying links.

There aren't really any other standardised XML-writing libraries; i
think you'd have to make a DOM tree and feed it to some kind of
serialiser via JAXP or something, which you don't want to do.

tom
Also, depending on the complexity of your requirements, it *might* be
worth wile to either use a templating language or manually building the
strings.
 
A

Arne Vajhøj

What are the best-regarded Java classes for writing XML these days?

I'm afraid I've been out of touch with XML for a few years and long ago lost
track of what is well-regarded in the Java community for creating XML files.

I have a need to write an XML file but NOT to work with it any further; the
file will be read by Microsoft Word but not processed any further. If
someone can point me to some suitable classes for writing XML - and,
ideally, a tutorial describing the proper use of these classes - I would
really appreciate that.

The best solution depends on the context.

You have a schema for your output: JAXB and generate Java classes,
populate objects and write to file.

You need to do some XML'ish work before you write the file: W3C DOM
(newer version supports writing as standard).

Very simple stuff: StAX is easy and fast.

Arne
 
M

markspace

Rhino said:
What are the best-regarded Java classes for writing XML these days?

I'm afraid I've been out of touch with XML for a few years and long ago lost
track of what is well-regarded in the Java community for creating XML files.

I have a need to write an XML file but NOT to work with it any further; the
file will be read by Microsoft Word but not processed any further. If
someone can point me to some suitable classes for writing XML - and,
ideally, a tutorial describing the proper use of these classes - I would
really appreciate that.


If you're going to program in Java, it would help to get some basic
references for these things for your bookshelf so you have them to refer
to. Maybe you do, and you just want to see what else might be out
there, but it kinda sounds like you need to get some more reference
material.

I'd recommend Learning Java, 3rd ed., by O'Reilly. Besides language
basics, LJ has a really huge number of chapters on the Java API,
including a nice section on XML. It covers the basics of the parsers
available, gives sample code for each one, and also gives a straight
forward comparison of each, which sounds like just what you need.

Taking the info from LJ:

SAX: low level, more complex but lots of control
http://java.sun.com/javase/6/docs/api/javax/xml/parsers/SAXParser.html

DOM: generic, somewhat non-Java-ish
http://72.5.124.55/javase/6/docs/api/org/w3c/dom/package-summary.html

JDOM: more Java-y, easier to use
http://www.jdom.org/docs/apidocs/

XPath: easiest, might be read-only though (no good for writing)
http://java.sun.com/javase/6/docs/api/javax/xml/xpath/XPathFactory.html

There's also XSLT to look at:
http://java.sun.com/javase/6/docs/api/javax/xml/transform/TransformerFactory.html

And also something called JAXB, which generates Java bindings to XML.
This might be the most interesting for you.
http://java.sun.com/developer/technicalArticles/WebServices/jaxb/


Good luck.
 
T

Tom Anderson

If you're going to program in Java, it would help to get some basic
references for these things for your bookshelf so you have them to refer to.
Maybe you do, and you just want to see what else might be out there, but it
kinda sounds like you need to get some more reference material.

I'd recommend Learning Java, 3rd ed., by O'Reilly. Besides language
basics, LJ has a really huge number of chapters on the Java API,
including a nice section on XML. It covers the basics of the parsers
available, gives sample code for each one, and also gives a straight
forward comparison of each, which sounds like just what you need.

You reckon? I'm not sure how much use any of the parser APIs are for
writing XML. Parsing is traditionally very much about reading. Which is
the opposite of writing.

tom
 
M

markspace

Tom said:
You reckon? I'm not sure how much use any of the parser APIs are for
writing XML. Parsing is traditionally very much about reading. Which is
the opposite of writing.

As far as I know, all of these parsers can also be used for writing,
except for XPath.
 
A

Arne Vajhøj

As far as I know, all of these parsers can also be used for writing,
except for XPath.

A parser is by definition reading.

But:

W3C DOM : reading + in recent versions writing
SAX : reading
JAXB : reading + writing
StAX : reading + writing
JDOM : reading + writing

So I would say 3.5 out of 5.

XPath is not a parser but a query tool to be used
with W3C DOM or JDOM.

Arne
 
J

Jeff Higgins

Rhino said:
What are the best-regarded Java classes for writing XML these days?

I'm afraid I've been out of touch with XML for a few years and long ago lost
track of what is well-regarded in the Java community for creating XML files.

I have a need to write an XML file but NOT to work with it any further; the
file will be read by Microsoft Word but not processed any further. If
someone can point me to some suitable classes for writing XML - and,
ideally, a tutorial describing the proper use of these classes - I would
really appreciate that.

Given the information you've provided,
some suitable Java classes are:
BufferedWriter
OutputStreamWriter
FileOutputStream
and the standard tutorials for these classes are here:
<http://java.sun.com/docs/books/tutorial/essential/io/>.

What do you really want to do?
 
J

Jeff Higgins

Stefan said:
Jeff Higgins said:
Given the information you've provided,

Here is a full-blown XML writer, writing a complete XML document:

public class XMLWriter
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( "<a/>" ); }}

My XML editor reports that the output of your XMLWriter is well-formed.

Rhino specified that he needs to write an XML file.

public class XMLFileWriter
{ public static void main( final java.lang.String[] args )
throws java.io.IOException
{ new java.io_OutputStreamWriter(
new java.io.FileOutputStream(args[0]),
java.nio.charset.Charset.forName("UTF-8")).append("<a/>"); }}
 
S

Stefan Ram

Jeff Higgins said:
Rhino specified that he needs to write an XML file.

Yes. Whether the standard output of a Java process is
associated with a file or some other entity depends on the
environment. (Under UN*X, the console might also be an entry
in the file system.) One also can use redirection:

java Main >file.xml

, but you are right insofar as this association to a file
would not be determined by the Java source code.
 
J

Jeff Higgins

Stefan said:
Yes. Whether the standard output of a Java process is
associated with a file or some other entity depends on the
environment. (Under UN*X, the console might also be an entry
in the file system.) One also can use redirection:

java Main >file.xml

, but you are right insofar as this association to a file
would not be determined by the Java source code.

I only hope that Rhino doesn't take our facetiousness as mean-spirited,
and that he will respond with an indication of where his content resides.
 
M

Mike Schilling

Arne said:
A parser is by definition reading.

But:

W3C DOM : reading + in recent versions writing
SAX : reading
JAXB : reading + writing
StAX : reading + writing
JDOM : reading + writing

So I would say 3.5 out of 5.

XPath is not a parser but a query tool to be used
with W3C DOM or JDOM.

XSLT can be used turn SAX events into an XML-formatted stream, though I've
never seen that used as a way of composing XML. Hmm.

import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLFilterImpl;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;

public class MakeXML
{
public static void main(String[] args) throws Exception
{
Transformer xf = TransformerFactory.newInstance().newTransformer();
xf.transform(
new SAXSource(new XMLEventGenerator(), null),
new StreamResult(System.out));
}

public static class XMLEventGenerator extends XMLFilterImpl
{
public void parse(String systemId) throws SAXException, IOException
{
generateEvents();
}

public void parse(InputSource input) throws SAXException,
IOException
{
generateEvents();
}

private void generateEvents() throws SAXException
{
ContentHandler ch = getContentHandler();
ch.startDocument();
ch.startElement(null, "start", "start", null);
ch.endElement(null, "start", "start");
ch.endDocument();
}
}
}

outputs :

<?xml version="1.0" encoding="UTF-8"?><start/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,064
Latest member
naturesElixirCBDReview

Latest Threads

Top