Java绑定与手动定义类

时间:2021-10-26 17:01:35

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.

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

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

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

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?

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

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

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

2 个解决方案

#1


14  

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. It is time consuming to write marshalling and unmarhsalling code, especially the first time.

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

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

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

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

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

  4. 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.

    您需要进行大量的单元测试,以确保您已经涵盖了可选元素和属性领域的所有基础。

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为我做的”。多年来我已经完成了这项工作,我会说JAXB为您节省了大量的时间和精力,尤其是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生成的类泄漏到您的应用程序中。

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(有很多理由说明你将来可能会这样做)那么你将面临一项具有挑战性和痛苦的任务(相信我,我们已经完成了它,我们永远不会进入那个位置再次)。

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.

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

#2


0  

I do not want to start a long and endless holly war on the question but from my experience - if you can use tool that generates all code for you and do not require JAR dependable. The code that will be generated will be ugly but it will work until you will clearly see a need to handcraft the handwritten code. In most cases you will not need to do that. I am sorry I can not give an instant recommendation on what tool to use.

我不想在这个问题上开始漫长而无休止的冬青战争,但是根据我的经验 - 如果你可以使用为你生成所有代码的工具,并且不需要JAR可靠。将生成的代码将是丑陋的,但它将工作,直到您将清楚地看到需要手工编写手写代码。在大多数情况下,您不需要这样做。对不起,我无法立即建议使用什么工具。

#1


14  

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. It is time consuming to write marshalling and unmarhsalling code, especially the first time.

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

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

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

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

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

  4. 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.

    您需要进行大量的单元测试,以确保您已经涵盖了可选元素和属性领域的所有基础。

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为我做的”。多年来我已经完成了这项工作,我会说JAXB为您节省了大量的时间和精力,尤其是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生成的类泄漏到您的应用程序中。

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(有很多理由说明你将来可能会这样做)那么你将面临一项具有挑战性和痛苦的任务(相信我,我们已经完成了它,我们永远不会进入那个位置再次)。

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.

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

#2


0  

I do not want to start a long and endless holly war on the question but from my experience - if you can use tool that generates all code for you and do not require JAR dependable. The code that will be generated will be ugly but it will work until you will clearly see a need to handcraft the handwritten code. In most cases you will not need to do that. I am sorry I can not give an instant recommendation on what tool to use.

我不想在这个问题上开始漫长而无休止的冬青战争,但是根据我的经验 - 如果你可以使用为你生成所有代码的工具,并且不需要JAR可靠。将生成的代码将是丑陋的,但它将工作,直到您将清楚地看到需要手工编写手写代码。在大多数情况下,您不需要这样做。对不起,我无法立即建议使用什么工具。