2012-01-31 5 views
3

xjc를 사용하여 몇 가지 클래스를 만들었습니다.JAXB - List <Serializable>?

public class MyType { 

    @XmlElementRefs({ 
     @XmlElementRef(name = "MyInnerType", type = JAXBElement.class, required = false), 

    }) 
    @XmlMixed 
    protected List<Serializable> content; 

    public List<Serializable> getContent() { 
     if (content == null) { 
      content = new ArrayList<Serializable>(); 
     } 
     return this.content; 
    } 
} 

하지만 난 MyInnerType가 직렬화 가능하지 않기 때문에

getContent().add(newItem); 

를 사용하여 내부 요소를 추가 기운 다. 오브젝트 목록이 아닌 이유는 무엇입니까? 내부 요소를 추가하려면 어떻게해야합니까?

+0

: 2 링크에서

? – Tudor

+0

MyInnerType이 Serializable이 아닙니다. – bunnyjesse112

답변

4

herehere (사용자의 시나리오를 해결해야합니다)을 참조하십시오. 당신이지고 어떤 오류

<!-- schema fragment having mixed content --> 
<xs:complexType name="letterBody" mixed="true"> 
<xs:sequence> 
    <xs:element name="name" type="xs:string"/> 
    <xs:element name="quantity" type="xs:positiveInteger"/> 
    <xs:element name="productName" type="xs:string"/> 
    <!-- etc. --> 
</xs:sequence> 
</xs:complexType> 
<xs:element name="letterBody" type="letterBody"/> 


// Schema-derived Java code: 
// (Only annotations relevant to mixed content are shown below, 
// others are ommitted.) 
import java.math.BigInteger; 
public class ObjectFactory { 
    // element instance factories 
    JAXBElement<LetterBody> createLetterBody(LetterBody value); 
    JAXBElement<String>  createLetterBodyName(String value); 
    JAXBElement<BigInteger> createLetterBodyQuantity(BigInteger value); 
    JAXBElement<String>  createLetterBodyProductName(String value); 
    // type instance factory 
    LetterBody> createLetterBody(); 
} 

public class LetterBody { 
    // Mixed content can contain instances of Element classes 
    // Name, Quantity and ProductName. Text data is represented as 
    // java.util.String for text. 
    @XmlMixed 
    @XmlElementRefs({ 
      @XmlElementRef(name="productName", type=JAXBElement.class), 
      @XmlElementRef(name="quantity", type=JAXBElement.class), 
      @XmlElementRef(name="name", type=JAXBElement.class)}) 
    List getContent(){...} 
} 
+0

고마워! 정말 나를 도왔다! – bunnyjesse112

+0

이것은 정말로 도움이되었습니다. 링크 주셔서 감사합니다! –

+0

링크가 끊어졌습니다. 새로운 링크를 부탁드립니다. – ulab

1

거기에 무엇을 추가해야한다고 생각하십니까? 나는 비슷한 세대를 사용했고 이와 같은 분야를 가지고 있었고 기대는 그것이 String content가 될 것이라는 것이었다.

아마도 이것이 xsd가 생성 된 것을 보여주는 데 도움이 될 것입니다.

+0

답변 해 주셔서 감사합니다. MyInnerType는 String가 아닙니다. 왜리스트 가 생성되고리스트는 이 아닌가? JAXBElement 등에서 MyInnerType을 랩핑해야합니까? – bunnyjesse112

+1

개체가 직렬화되지 않기 때문에 좋은 생각 인 것 같습니다. 하지만 왜 객체를 직렬화 할 수 없습니까? 그것은 단지 인터페이스 구현의 문제입니다. – AHungerArtist