2013-02-14 1 views
1

단일 속성의 각 값은 스칼라 (String, Integer, ...) 또는 a 스칼라 컬렉션 (Collection, Collection, ...). 여기에 예제와 같은 역할을하는 XML 문서입니다 :JAXB/MOXy : 스칼라 또는 스칼라 목록을 포함하는 필드의 언 마샬링 방법

<properties xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:java="http://java.oracle.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <property name="test#1" xsi:type="xs:int">1</property> 
    <property name="test#2" xsi:type="java:int[]"> 
     <value xsi:type="xs:int">1</value> 
     <value xsi:type="xs:int">2</value> 
     <value xsi:type="xs:int">3</value> 
    <value xsi:type="xs:int">4</value> 
    </property> 
</properties> 

이 내가 사용하고자하는 클래스는,하지만 난 방법을 제대로 값 필드에 태그를 소비하고이 구조를 생산하기 위해 아무 생각이 없습니다. 스칼라 또는 스칼라 목록의 형식으로 속성 요소의 구문 분석 된 내용을 포함해야합니다. 데이터 유형은 속성 값으로 제공됩니다. 두 개의 필드 protected Object scalarprotected List<Object> list, 하나 @XmlValue 태그를 사용

@XmlRootElement(name = "property") 
public class Property { 

    @XmlAttribute(name = "name") 
    protected String name; 
    @??? 
    protected Object value; 
} 

, @XmlElement(name = "value")와 다른 하나는 작동하지 않습니다.

누구에게 아이디어가 있습니까? 다음과 같이

UPDATE 1

나는 Property 태그 :

@XmlRootElement(name = "property") 
public class Property { 

    @XmlAttribute(name = "name") 
    protected String name; 
    @XmlJavaTypeAdapter(Test2Adapter.class) 
    @XmlPath(".") 
    protected Object value; 
} 

내가 부분적으로 다음과 같은 어댑터 클래스를 구현 한

public class Test2Adapter extends XmlAdapter<AdaptedValue, Object> { 

@Override 
public Object unmarshal(AdaptedValue v) throws Exception { 
    if (v instanceof Scalar) { 
    return ((Scalar) v).value; 
    } 
    if (v instanceof Complex) { 
     return ((Complex) v).value; 
    } 
    return null; 
} 

@Override 
public AdaptedValue marshal(Object v) throws Exception { 
    if (v instanceof String) { 
    Scalar s = new Scalar(); 
    s.value = v; 

    return s; 
    } 
if (v instanceof Collection) { 
    Complex c = new Complex(); 
    c.value = (Collection<? extends Object>) v; 

    return c; 
} 
return null; 
} 

AdaptedValue :

@XmlSeeAlso({ Scalar.class, Complex.class }) 
public abstract class AdaptedValue { 
} 

스칼라 :

public class Scalar extends AdaptedValue { 

    @XmlValue 
    public Object value; 

} 

단지 :

public class Complex extends AdaptedValue { 

    @XmlAttribute(name = "xsi:type") 
    public String type; 

    @XmlElement(name = "value") 
    public Collection<? extends Object> value 
} 

모든 것이 올바르게 정렬 화되어 있지만, 비 정렬 화 작동하지 않습니다. 내가 잘못 아니에요 경우

javax.xml.bind.UnmarshalException 
- with linked exception: 
[Exception [EclipseLink-43] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): 
org.eclipse.persistence.exceptions.DescriptorException 
Exception Description: Missing class for indicator field value [xs:string] of type[class java.lang.String]. 
Descriptor: 
XMLDescriptor(com.materna.osgi.service.cm.rest.resource.representation.Test2Adapter$AdaptedValue --> [])] 
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:190) 

답변

0

, 당신은 XmlJavaTypeAdapter와라는 뭔가가 필요 : 나는 다음과 같은 예외를 얻을. 이 코드는 오래전에 사용되었지만 지금 당장은 코드를 작성하지 않아도됩니다. 링크를 살펴 보시고 여전히 고민이 있다면 나에게 외쳐보십시오. 위 게시물의 코멘트 섹션에서

http://weblogs.java.net/blog/kohsuke/archive/2005/04/xmladapter_in_j.html

는 "pomcompot는"목록/컬렉션에 대한 래퍼를 사용하여 언급하고있다. 나는 래퍼 클래스를 사용하여 기억하고 그것은 작동했습니다.

http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp#XmlJavaTypeAdapter