JAXB 工作流程 - 如何开始使用 JAXB

发布于 2024-09-12 21:09:48 字数 487 浏览 5 评论 0原文

JAXB 文档就像一本教科书,我根本不需要在需要使用 JAXB 之前花时间学习它的所有内容。

我有一个 XSD,如果我想使用 JAXB 来编组和取消编组,工作流程是什么?

我不需要任何细节,只需要一个高层次的视图。

我已经知道的: 1. JAXB 可用于获取对象并从中创建 XML 文档,反之亦然 2.它使用了一些名为“XJC”的神秘工具,我在他们的网站上找不到任何地方的下载 3、XJC基于XSD为您创建课程

首先,如何找到XJC?我实际上知道它安装在我的系统上,但不确定我从哪里得到它。
其次,无论这个工具是什么以及它如何到达我的机器,如何使其与最新版本的 JAXB 一起运行? 第三,如果我的 XSD 发生更改,我是否真的必须重新创建整个 Java 对象,从而可能使我的所有测试无效?或者我可以定义对象到 XSD 映射,以便我可以控制映射,而不是某些默认代码生成吗?

我主要习惯于采用 XML 并使用 xStream 之类的东西来手动解组,但这不再是我的目的的选择。

The JAXB documentation is like a text-book, and I simply don't have to time to learn everything JAXB before I need to use it.

I have an XSD, if I want to use JAXB to marshal and un-marshal what is the workflow?

I don't need any specifics just a high level view.

What I know already:
1. JAXB can be used to take objects and create XML documents from them and vice versa
2. It uses some mysterious tool named "XJC" which I haven't been able to find a download of ANYWHERE on their website
3. XJC creates classes for you based on XSD

First, How do I find XJC? I actually know that it's installed on my system, not sure where I got it from though.
Second, whatever this tool is and how it got to my machine, how do I make it run with the most up to date version of JAXB?
Third, so if my XSD changes do I really have to recreate the whole Java object, therefore possibly invalidating all my tests? Or can I define object to XSD mappings so that I'm in control of the mapping, not some default code generation?

I'm mostly used to taking XML and using something like xStream to manually unmarshal, but that's not an option for my purposes anymore.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-09-19 21:09:48

假设你有JDK6;

xjc 位于 JDK 的 bin/ 文件夹中,类似于 C:\Program Files (x86)\Java\jdk1.6.0_20\bin\xjc.exe

JDK 附带 jaxb,而可能有来自 http://jaxb.java.net/ 或其他可用的实现,您可能不需要关心这个在此刻。

JAXB 可以做很多事情,我不完全确定你想要完成什么。如果您有一个xsd,您可以通过运行eg 来从中生成java 类

xjc -p com.mypackage myschema.xsd

,并将生成的类包含在您的项目中(更典型的是,您可以将其作为构建过程的一部分来运行)。

或者我可以定义对象到 XSD 映射吗
这样我就能控制映射,
不是一些默认的代码生成吗?

生成的类只是带有一些注释的 pojo,您可以自己创建这些类并通过注释完全控制映射。

如前所述,jaxb 可以做很多事情,这里只是一些基本示例,SearchParam/SearchType 是 xjc 从小型自定义 .xsd 生成的类

将对象序列化为 XML 文件

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
List<String> hours = new LinkedList<String>();
hours.add("2009.12.11");
hours.add("2009.12.13");

SearchParam param = new SearchParam();
param.setFilter("greater");
param.setHours(hours);
param.setSearchType(SearchType.Fuzzy);
marshaller.marshal(param, new FileWriter("/tmp/SearchParam.xml"));    

反序列化一个 xml 文件

 JAXBContext context = JAXBContext.newInstance(SearchParam.class);
 Unmarshaller unMarshaller = context.createUnmarshaller();
 SearchParam param = (SearchParam) unMarshaller.unmarshal(
                    new FileInputStream("/tmp/SearchParam.xml"));

反序列化并进行架构验证

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                 .newSchema(new  File("/tmp/schema1.xsd"));
unMarshaller.setSchema(schema);
SearchParam param = unMarshaller.unmarshal(
                      new FileInputStream("/tmp/SearchParam.xml"));

更多信息

Assuming you have JDK6;

xjc is in the bin/ folder of your JDK, something like C:\Program Files (x86)\Java\jdk1.6.0_20\bin\xjc.exe

The JDK comes with jaxb, while there might be newer versions around from http://jaxb.java.net/ or other implementations available, you probably don't need to concern yourself with that at this point.

JAXB can do a lot of things, I'm not entierly sure exactly what you're trying to accomplish. If you have an xsd, you can generate java classes from it by running e.g.

xjc -p com.mypackage myschema.xsd

And include the generated classes in your project (More typically you'd run that as part of your build process).

Or can I define object to XSD mappings
so that I'm in control of the mapping,
not some default code generation?

The generated classes are just pojos with some annotations, you could create those classes yourself and be in full control of the mapping via annotations.

As said, jaxb can do a lot of things, here's just some basic examples, SearchParam/SearchType is a class generated by xjc from a small custom .xsd

Serialize an object to an XML file

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
List<String> hours = new LinkedList<String>();
hours.add("2009.12.11");
hours.add("2009.12.13");

SearchParam param = new SearchParam();
param.setFilter("greater");
param.setHours(hours);
param.setSearchType(SearchType.Fuzzy);
marshaller.marshal(param, new FileWriter("/tmp/SearchParam.xml"));    

Deserialize an xml file

 JAXBContext context = JAXBContext.newInstance(SearchParam.class);
 Unmarshaller unMarshaller = context.createUnmarshaller();
 SearchParam param = (SearchParam) unMarshaller.unmarshal(
                    new FileInputStream("/tmp/SearchParam.xml"));

Deserialize and do schema validation

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                 .newSchema(new  File("/tmp/schema1.xsd"));
unMarshaller.setSchema(schema);
SearchParam param = unMarshaller.unmarshal(
                      new FileInputStream("/tmp/SearchParam.xml"));

More information

以为你会在 2024-09-19 21:09:48

您可能已经安装了 XJC。检查 JDK 6 安装的 bin 目录。一旦找到,如果您只运行 XJC,它将为您提供命令行选项。

JAXB 是一个规范,因此有多种实现:

如果您修改架构,您将能够调整您的对象模型,尤其是使用 MOXy 扩展。

要开始使用,请参阅:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

Chances are you already have XJC installed. Check the bin directory of your JDK 6 install. Once found if you just run XJC it will give you the command line options.

JAXB is a spec, so there are multiple implementations:

If you modify your schema you will be able to tweak your object model, especially with the MOXy extensions.

To get started see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文