JAXB xjc映射到现有域对象

编程入门 行业动态 更新时间:2024-10-26 04:21:30
本文介绍了JAXB xjc映射到现有域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做了很多搜索,但找不到如何将XML架构映射到现有域对象的简明示例,而不是使用 xjc 创建全新的域架构。我已经创建了一个绑定(xjb)文件,但仍然无法找到完成此任务。

I have done a lot of searching and cannot find a concise example of how to map an XML schema to existing domain objects instead of creating brand new ones utilizing xjc. I have created a bindings (xjb) file but still can find no way of accomplishing this.

如果我有一个现有的域对象,我希望JAXB使用,如以下:

If I have an existing domain Object that I want JAXB to use such as the following:

package com.blah.domain; class CustomerOffice{ private int id; private String name; private String phone; }

我有一个类似以下的XML Schema:

And I have an XML Schema like the following:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:www="www.blah" xmlns:xs="www.w3/2001/XMLSchema" targetNamespace="www.blah" elementFormDefault="unqualified"> <xs:element name="Customer"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:int" nillable="false" minOccurs="1" maxOccurs="1"/> <xs:element name="name" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="CustomerOffice" type="www:CustomerOffice" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="CustomerOffice"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="length" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema>

如果我用 xjc 生成JAXB类,它将创建一个名为 Customer 的新类(我想要的)。它还将创建一个名为 CustomerOffice 的新类(我不想要它,我希望它使用我现有的域对象)。

If I go to generate the JAXB classes with xjc it will create a new class called Customer (which I want). It will also create a new class called CustomerOffice (which I don't want, I want it to use my existing domain object).

因此,我希望它使用现有的 com.blah.domain.CustomerOffice 而不是指向type:www:CustomerOffice的模式。

So instead of the schema pointing to "type:www:CustomerOffice" I would want it to use the existing com.blah.domain.CustomerOffice.

我试着尽可能简单地做一个例子,感谢任何帮助。

I tried to make this as simple an example as possible, any help is appreciated.

推荐答案

你可以使用外部绑定文件,以配置XJC做你想要的。

You can use an external binding file to configure XJC to do what you want.

<jxb:bindings xmlns:xs="www.w3/2001/XMLSchema" xmlns:jxb="java.sun/xml/ns/jaxb" version="2.1"> <jxb:bindings schemaLocation="yourSchema.xsd"> <jxb:bindings node="//xs:complexType[@name='CustomerOffice']"> <jxb:class ref="com.blah.domain.CustomerOffice"/> </jxb:bindings> </jxb:bindings> </jxb:bindings>

XJC致电

xjc -d outputDir -b binding.xml yourSchema.xsd

更多推荐

JAXB xjc映射到现有域对象

本文发布于:2023-07-16 04:18:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   JAXB   xjc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!