2016-06-29 2 views
1

JAXB를 통해 상응하는 JAVA 클래스를 생성하기 위해 사전 정의 된 xsd 스키마 (불행히도 수정할 수 없음)가 있습니다. 현재 다음과 같이 정의 된 복잡한 유형으로 고민하고 있습니다. 상기 결합없이 xjc를 프로세스를 실행할 때JAXB anyType에서 문자열 컨텐츠에 액세스하기

<attribute id="address"> 
    <example:Address xmlns:example="http://example.com/ns"> 
     <Street>100 Nowhere Street</Street> 
     <City>Fancy</City> 
     <State>DC</State> 
     <Zip>99999</Zip> 
    </example:Address> 
</attribute> 

이 추천

<attribute id="myValue">201</attribute> 

뿐만 아니라 임베디드 XML : 구비

<xsd:complexType name="AttributeType"> 
    <xsd:complexContent> 
     <xsd:extension base="xsd:anyType"> 
     <xsd:attribute name="id" type="xsd:anyURI" use="required"/> 
     <xsd:anyAttribute processContents="lax"/> 
     </xsd:extension> 
    </xsd:complexContent> 
    </xsd:complexType> 

는 XML 예는, 이와 같은 직접 캐릭터 컨텐츠를 허용 수정하면 다음과 같은 수업을 받게됩니다.

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "AttributeType", propOrder = { 
    "any" 
}) 
public class AttributeType { 

    @XmlAnyElement 
    protected List<Element> any; 
    @XmlAttribute(name = "id", required = true) 
    @XmlSchemaType(name = "anyURI") 
    protected String id; 
    @XmlAnyAttribute 
    private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

    // getter setter omitted 
} 

이 문제는 첫 번째 예제의 문자열 내용을 가져올 수 없다는 것입니다. 이것은 XSD anytype and JAXB을 참조 할 수도 있지만 실제로 XSD를 수정하지 않고이를 달성 할 수 있는지는 알 수 없습니다. 그렇다면 어떻게 문자열 내용을 얻을 수 있습니까? Btw. 소스를 생성하기 위해 maven cxf-codegen-plugin을 사용하고 있습니다.

+0

문제를 해결할 수 있었습니까? 참조 된 링크 [link] (https://stackoverflow.com/questions/3488141/xsd-anytype-and-jaxb)는 내가 가지고있는 문제와 완전히 똑같습니다.이를 극복하는 방법을 찾으려고합니다. 그곳에서 해답을 찾지 못했습니다. – JGlass

답변

0

나는이 문제가 생성 된 매핑이 텍스트가 아닌 자식 요소를 찾는다는 사실에서 비롯된 것이라고 생각한다. 당신이 당신의 소스 코드 변경을 수정할 줄 수있는 경우에

...

<xsd:complexType name="AttributeType"> 
    <xsd:complexContent mixed="true"> 
     <xsd:extension base="xsd:anyType"> 
     <xsd:attribute name="id" type="xsd:anyURI" use="required"/> 
     <xsd:anyAttribute processContents="lax"/> 
     </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 

을하지만 이후 당신은 할 수 없습니다 : 당신이 당신의 XSD를 수정할 수 있다면

, 해결책은 될 것

@XmlAnyElement 
protected List<Element> any; 

@XmlAnyElement 
@XmlMixed 
protected List<Object> any; 

개체 목록은 하위 요소의 경우 Element이고 텍스트의 경우 String이어야합니다.

+0

생성 된 소스를 변경하는 것은 XSD가 "자주"업데이트되어 빌드 프로세스에서 생성되므로 최적의 솔루션이 아닙니다. 바인딩 수정을 통해이 수정을 수행 할 수 있습니까? – Ingo