col*_*jax 10 xml schema bind jaxb xjc
我做了很多搜索,找不到如何将XML模式映射到现有域对象的简明示例,而不是使用xjc创建全新的.我已经创建了一个绑定(xjb)文件但仍无法找到完成此操作的方法.
如果我有一个我希望JAXB使用的现有域对象,如下所示:
package com.blah.domain;
class CustomerOffice{
private int id;
private String name;
private String phone;
}
Run Code Online (Sandbox Code Playgroud)
我有一个类似以下的XML Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:www="http://www.blah.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.blah.com" 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>
Run Code Online (Sandbox Code Playgroud)
如果我用xjc生成JAXB类,它将创建一个名为Customer的新类(我想要).它还将创建一个名为CustomerOffice的新类(我不想要它,我希望它使用我现有的域对象).
因此,我希望它使用现有的com.blah.domain.CustomerOffice而不是指向"type:www:CustomerOffice"的模式.
我试着尽可能简单地做一个例子,感谢任何帮助.
bdo*_*han 14
您可以使用外部绑定文件将XJC配置为执行您想要的操作.
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/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>
Run Code Online (Sandbox Code Playgroud)
XJC电话
xjc -d outputDir -b binding.xml yourSchema.xsd
Run Code Online (Sandbox Code Playgroud)