You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Reinier Zwitserloot <re...@zwitserloot.com> on 2005/12/04 05:59:48 UTC

annotations-based XML parsing for java?

I'm not sure if this idea is new, but over the weekend I developed a
rather simple to use XML parser that uses annotations and POJOs (plain
old java objects) to directly load XML data into objects.

I was wondering if the apache xml team would be interested in such a
thing, as a simpler cousin to xerces (actually, you can do virtually
everything, except process generic unknown XML, with the annotation
idea - that covers just about everything except XML editors, which
you'd have to write using DOM/Xerces. it also abstracts away the major
differences between DOM and SAX; it can be used in both 'event-based'
and 'fully loaded in memory' configurations with virtually exactly the
same code).

I've hacked a tutorial together that takes you through creating a
parser for ATOM in about 10 minutes, here:

http://www.zwitserloot.com/java-boilerplate/mox/tutorial.html

I don't have any particular wishes as to what happends to the code
that's there - public domain, or apache license, it's all fine by me.
java 1.5 required, obviously - annotations make it tick.

--
-------------------
"Ubi non accusator, ibi non judex."

 -- Reinier Zwitserloot

---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Re: annotations-based XML parsing for java?

Posted by Reinier Zwitserloot <re...@zwitserloot.com>.
>
> I haven't read through it in detail yet, but it looks like you've simply
> invented yet another data binding framework. Perhaps unique is that
> you've used annotations here, butn otherwise what does this do that
> several other databinding frameworks (JAXB, Castor, Zeus, etc.) don't?

I've skimmed JAXB before, but perhaps a bit too hastily. Castor and
Zeus are news to me. To be honest, the flurry of SAX/DOM (JDOM, DOM4J,
java's own DOM, Xalan)... made it somewhat difficult to figure out
there are in fact other streams of thought on how to parse XML out
there.

I've looked at those 3 binding frameworks, and you're basically right,
this tool does not do significantly more, though I would suggest to
the authors of those tools they also offer annotations-based
configuration - in various circumstances it's just easier to maintain
all of it as code instead of a bunch of separate XML documents (ie:
for some simple developer, to use JAXB, he needs to learn schema). A
10 minute tutorial would also not go amiss - before I managed to
figure out exactly what JAXB can do, an hour had passed. But, that's a
documentation complaint.

The main new thing MOX appears to bring to the table is that you don't
need to know very much about XML to use it. For example, knowledge of
XML Schema or DTDs are not required; define your XML structure as
familiar java classes with some annotations tacked on, and there's
your simplified 'schema'. "Validation" occurs automatically - if the
input XML can't be stuffed into the classes you wrote, MOX throws an
exception with the culprit as message. Yes, it isn't quite as
flexible, but it allows being very proficient at reading and writing
XML documents without any serious XML knowledge.

In other words, none of the tools out there so far are very friendly
to the layman.

Then again, those tend to be using other programming languages,
perhaps, such as PHP, ruby, or python.


>
> The limitation you advertise is a common problem with data binding
> frameworks. They are very rigid and inflexible, and can't handle the
> extensibility of XML. Properly written DOM or SAX (or XOM) code does
> much better. XSLT does much better still.

If MOX (and I'm guessing JAXB, Castor, and Zeus as well?) encounters
unknown tags or attributes, they get skipped - or, I can add an
annotation that allows unknown tags to get sent as either a DOM
Document, or as a plain string, to a special 'unknown tag' handler
method. It's a rather basic support for extensibility, but most
applications using  XML formats for data exchange don't need any more
than that.

Stuff ordinary programmers just use their own internal data formats
for, or property files, or just serialized objects, because working
with a DOM parser is far too much hassle - that's where MOX (and
apparently the other 3 binding engines you've mentioned) shine.

 -- Reinier Zwitserloot

---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Re: annotations-based XML parsing for java?

Posted by Paul Libbrecht <pa...@activemath.org>.
Elliotte Harold wrote:
> According to the site, "MOX only works if you know the structure of 
> the XML beforehand. However, unless you are writing some sort of XML 
> editor, this is virtually always the case." I respectfully disagree, I 
> routinely write code for systems the full structure of which is only 
> determined reflectively at runtime. I even more commonly write code 
> where the structure is only somewhat specified in advance.
>
> The limitation you advertise is a common problem with data binding 
> frameworks. They are very rigid and inflexible, and can't handle the 
> extensibility of XML. Properly written DOM or SAX (or XOM) code does 
> much better. XSLT does much better still

I'd like to second that strongly at least for updatability: changing a 
Schema (DTD, or whatever structure representation) and associated 
documents is quite common. We keep doing this in ActiveMath. Changing 
the POJOs to match is a bit more delicate (and typically has influence 
much deeper into the code than just the beans).

Allow me to add that XPath (which can be used in XOM, DOM, JDOM, XSLT, 
DOM4j and many others) is the best flexibility and readability you can 
afford. The performance is smaller, indeed (but not enormous), but the 
manageability is much greater!

paul


---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Re: annotations-based XML parsing for java?

Posted by Elliotte Harold <el...@metalab.unc.edu>.
Reinier Zwitserloot wrote:
> I'm not sure if this idea is new, but over the weekend I developed a
> rather simple to use XML parser that uses annotations and POJOs (plain
> old java objects) to directly load XML data into objects.

I haven't read through it in detail yet, but it looks like you've simply 
invented yet another data binding framework. Perhaps unique is that 
you've used annotations here, butn otherwise what does this do that 
several other databinding frameworks (JAXB, Castor, Zeus, etc.) don't?

According to the site, "MOX only works if you know the structure of the 
XML beforehand. However, unless you are writing some sort of XML editor, 
this is virtually always the case." I respectfully disagree, I routinely 
write code for systems the full structure of which is only determined 
reflectively at runtime. I even more commonly write code where the 
structure is only somewhat specified in advance.

The limitation you advertise is a common problem with data binding 
frameworks. They are very rigid and inflexible, and can't handle the 
extensibility of XML. Properly written DOM or SAX (or XOM) code does 
much better. XSLT does much better still.

-- 
Elliotte Rusty Harold  elharo@metalab.unc.edu
XML in a Nutshell 3rd Edition Just Published!
http://www.cafeconleche.org/books/xian3/
http://www.amazon.com/exec/obidos/ISBN=0596007647/cafeaulaitA/ref=nosim

---------------------------------------------------------------------
To unsubscribe, e-mail: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org