From: Rhino on
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.

--
Rhino


From: Tom Anderson on
On Wed, 3 Mar 2010, Rhino wrote:

> 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

--
alle Menschen werden Br
From: Daniel Pitts on
On 3/3/2010 1:54 PM, Tom Anderson wrote:
> On Wed, 3 Mar 2010, Rhino wrote:
>
>> 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
>
Also, depending on the complexity of your requirements, it *might* be
worth wile to either use a templating language or manually building the
strings.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on
On 03-03-2010 16:42, Rhino wrote:
> 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
From: markspace on
Rhino wrote:
> 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.