2012-05-02 5 views
8

많은 검색을 수행했으며 xjc을 사용하여 새로운 스키마를 만드는 대신 XML 스키마를 기존 도메인 객체에 매핑하는 방법에 대한 간결한 예제를 찾을 수 없습니다. 바인딩 (xjb) 파일을 만들었지 만이를 수행 할 수있는 방법이 없습니다.JAXB xjc 기존 도메인 객체에 매핑

난 JAXB는 다음과 같은 사용하려는 기존 도메인 개체가있는 경우 :

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

을 그리고 난 같은 XML 스키마가 다음

<?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> 

내가 이동하는 경우 xjc으로 JAXB 클래스를 생성하면 고객이라는 새로운 클래스가 생성됩니다. 또한 CustomerOffice이라는 새 클래스를 만듭니다 (원하지 않는 경우 기존 도메인 객체를 사용하고 싶습니다).

그래서 대신 "유형 : WWW : CustomerOffice"가 가리키는 스키마 나는 그것이 기존의 com.blah.domain.CustomerOffice 사용하고자하는 것입니다.

가능한 한 간단한 예를 만들려고했는데 도움이되었습니다.

답변

11

외부 바인딩 파일을 사용하여 원하는대로 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> 

XJC 전화는

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

완벽하게 일했다! 고맙습니다, 잘하면이 일로 인해 어려움을 겪은 다른 사람들에게 도움이 될 것입니다. – colbyjax

관련 문제