Java绑定与手动定义类 [英] Java Binding Vs Manually Defining Classes

查看:94
本文介绍了Java绑定与手动定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML模式,我需要为其创建Java类。它不是一个特别大的架构,我会说它将导致大约20个类。我试图权衡是否使用自动绑定程序(如JAXB或JiBX中提供的程序),或者是否手动编写我自己的类并使用XStream之类的内容进行编组/解组。

I have a XML schema that I will need to create Java classes for. It is not a particularly large schema, I'd say it will result in around 20 classes. I am trying to weigh up whether to use an automatic binding program (like the one supplied in JAXB or JiBX) or whether to manually write my own classes and use something like XStream for marshalling/unmarshalling.

编写自己的类而不是使用绑定程序有什么优点/缺点。

What are the advantages/disadvantages of writing your own classes as opposed to using a binding program.

,我使用一个绑定程序,我永远与之相关。例如,如果我使用JAXB的绑定编译器来创建类,我是否必须使用JAXB进行所有编组/解组?

Also, one I use a binding program, am I tied to that forever. For example, if I use JAXB's binding compiler to create the classes, do I have to use JAXB for all marshalling/unmarshalling?

p.s。我已经看到了关于XML绑定/序列化的以下问题,这些问题很有用,但没有完全回答我的问题:
xml-serialization-in-java java-xml-binding

p.s. I've seen the following questions about XML binding/serialization, which were useful, but didn't answer my question totally: xml-serialization-in-java and java-xml-binding

推荐答案

我认为你的问题没有明确的答案。但我可以给你一些来之不易的建议。以下是需要考虑的事项:

I don't think there is a definitive answer to your question. But I can give you some hard won advice. Here are some things to consider:


  1. 编写编组和解组代码非常耗时,尤其是第一次。

  1. It is time consuming to write marshalling and unmarhsalling code, especially the first time.

您将花费大量时间来学习DOM库的细微差别(Xerces或其等价物)。

You will have to spend quite a bit of time learning the nuances of your DOM library (Xerces or its equivalent).

有很多重复,所以你最终会被编写一些助手类。

There is lots of duplication so you'll eventually be driven to writing some helper classes.

你需要很多单元测试以确保您已经涵盖了可选元素和属性领域的所有基础。

You'll need lots of unit tests to make sure you've covered all of your bases in the area of optional elements and attributes.

寻找在那个列表中,很容易说这就是JAXB为我做的。多年来我已经完成了这项工作,我会说JAXB会节省你相当多的时间和精力,尤其是Java 5/6中JAXB的最新版本。

Looking at that list it is pretty easy to say "that's what JAXB does for me". Having done for this for a number of years I would say that JAXB saves you quite a bit of time and effort, especially the latest iteration of JAXB in Java 5/6.

但是如果你选择JAXB,我们已经学到了一个很难的方法:

But if you go with JAXB there is one lesson we've learned the hard way that I would like to pass along:

***不要让JAXB生成的类泄漏到您的应用程序中。

*** Do not to let the JAXB generated classes leak into your application.

正如您在问题中所说,这将整个应用程序与JAXB的工作方式联系起来。如果必须更换JAXB(有很多理由说明你将来可能会这样做的话)那么你将面临一项具有挑战性和痛苦的任务(相信我,我们已经完成了它,我们永远不会进入那个再次定位)。

As you said in your question, this ties your entire application to the way JAXB does thing. If JAXB has to be replaced (there are a number of reason why you might do that in the future) then you will be faced with a challenging and painful task (trust me, we've done it and we'll never get into that position again).

现在我们总是将JAXB生成的类隐藏在外观或工厂后面,从JAXB类映射到具有所需行为的我们自己的域POJO。当我们做JDBC时,我们会想到JAXB; JAXB只是另一种数据源,是从我们的域POJO获取数据的另一种方式。领域POJO是秘诀,我们控制它们的编码方式以及如何使用它们。 JAXB只是编组和解组的工具。

Now we always hide our JAXB generated classes behind a facade or a factory, mapping from the JAXB classes to our own domain POJOs that have the required behavior. We think of JAXB as we do JDBC; JAXB is just another data source, another way of getting data to and from our domain POJOs. The domain POJOs are the secret sauce and we control how they are coded and how they are used. JAXB is just a tool for marshalling and unmarshalling.

这篇关于Java绑定与手动定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆