开发者

What is the benefit of JAXB over XSLT?

I've been going over this in my head and trying out different approaches for a few days now and I've searched SO (see What is best practice in converting XML to Java object?) and also Google for using JAXB over XSLT but cannot find a suitable answer. Over the years I've used Castor, JIBX and XSLT for various projects, so I know a little bit about XML binding.

My problem is that I have a very flat XML structure and I want to unmarshall it to Java classes and persist them directly to a relational database (has to be Oracle). A small example...

<Output>
    <Channel>
        <channelId>1</channelId>
        <genreId>1</genreId>
    </Channel>
    <Channel>
        <channelId>2</channelId>
        <genreId>2</genreId>
    </Channel>
    <Genre>
        <genreId>1</genreId>
        <name>Movies</name>
    </Genre>
    <Genre>
        <genreId>2</genreId>
        <name>Sport</name>
    </Genre>
    <ChannelName>
        <channelId>1</channelId>
        <name>The Movie Channel</name>
    </ChannelName>
    <ChannelName>
        <channelId>2</channelId>
        <name>The Sport Channel</name>
    </ChannelName>
</Output>

What I really want for the XML above is just t开发者_Python百科wo simple annotated classes in a OneToOne relationship, that I can persist using JPA. Basically I want classes that reflect the database tables like so:

class Channel {
    Long id;
    String name;
    Genre genre;        
}

class Genre {
    Long id;
    String name;       
}

The XML will only ever be unmarshalled and I am really looking for the simplest solution to this problem which I realise can be a subjective question.

I am a fan of XSLT so am really asking if it's good design to use XSLT to bend the original XML into a better structure the more closely matches the code and then just use some very simple JAXB annotations to bind to my classes. Or should I opt for doing the "transformation" using JAXB which I think would involve more actual Java code, for example XMLAdapters and more annotations. Basically, what is the benefit, or what else does JAXB give me over XSLT?


JAXB is the Java object-to-XML binding API.

XSL-T transforms XML to XML.

They don't feel like the same thing to me. The end result might be XML, but the source is different in each case.

I think the answer depends on your comfort level with each technology. If the XSL-T and JAXB combination feels natural to you, by all means go in that direction - unless it's a runtime operation that's performed repeatedly. In that case I'd say that performance might be a concern, depending on the complexity of the transformations.


I'd suggest taking a look at XStream, specifically its Converters API.

XStream provides a very simple way to serialize or deserialize XML to Java, without needing to provide a XSD (which I believe JAXB will require).


Take a look at ReXSL, a web development framework that integrates JAXB and XSL, on top of JAX-RS. The site generates every page as a JAXB-annotated object, which is converted to XML by the framework. Then, this XML is delivered to the browser with attached XSL stylesheet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜