2014-06-17 2 views
0

내가 가진 클래스JAXB 비 정렬 화 아이 @XmlIDREF

또한
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "customerOrder", propOrder = { 
    "demandsUuid", 
    "invoicesOutUuid", 
    "paymentsUuid", 
    "customerOrderPosition", 
    "purchaseOrdersUuid" 
}) 
public class CustomerOrder extends LegendEntity { 
    @XmlAttribute(name = "sourceAgentUuid") 
    @XmlIDREF 
    @XmlSchemaType(name = "IDREF") 
    @XmlJavaTypeAdapter(XmlAdapterTest.class) 
    protected Object sourceAgentUuid; 
.... 

에이전트 클래스

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "agent", propOrder = { 
    "attribute", 
    "requisite", 
    "contact", 
    "contactPerson", 
    "agentNewsItem", 
    "tags" 
}) 
@XmlSeeAlso({ 
    Company.class 
}) 
public class Agent 
    extends LegendEntity 
{ 

    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlID 
    @XmlSchemaType(name = "ID") 
    protected String uuid; 

그리고 비 정렬 화 XML snipetpets

<?xml version="1.0" encoding="UTF-8"?> 
<customerOrder deliveryPlannedMoment="2014-04-23T16:10:00+04:00" reservedSum="0.0" stateUuid="44b3fca5-a2b3-11e3-31b2-002590a28eca" targetAgentUuid="9a3c7d6b-4245-11e3-24c6-7054d21a8d1e" 
sourceAgentUuid="ef0b03de-c95b-11e3-9183-002590a28eca" sourceStoreUuid="b05ed064-8743-11e3-0c1a-002590a28eca" 
applicable="true" moment="2014-04-21T17:50:00+04:00" payerVat="true" rate="1.0" vatIncluded="true" created="2014-04-21T17:50:44.142+04:00" createdBy="[email protected]" name="440" updated="2014-04-22T12:19:56.472+04:00" updatedBy="[email protected]" readMode="SELF" changeMode="NONE"> 
       <accountUuid>5f65cc9e-3708-11e3-bf9a-7054d21a8d1e</accountUuid> 
       <accountId>5f65cc9e-3708-11e3-bf9a-7054d21a8d1e</accountId> 
       <uuid>ef1267b8-c95b-11e3-aeb4-002590a28eca</uuid> 
       <groupUuid>9a3ad96b-4245-11e3-010e-7054d21a8d1e</groupUuid> 
       <groupId>9a3ad96b-4245-11e3-010e-7054d21a8d1e</groupId> 
       <code>440</code> 
       <externalcode>440</externalcode> 

내가

을 언 마샬링하려고
JAXBElement poe = (JAXBElement)u.unmarshal(new FileInputStream("org_order.xml")); 
CustomerOrder co=(CustomerOrder)poe.getValue(); 
System.out.println(co.getSourceAgentUuid()); 

결과 널 (다른 모든되지 @XmlIDREF 필드 정렬 화 OK) 내가 ef0b03de-c95b-11e3-9183-002590a28eca 결과를 얻을 수있는 방법

?

간단하게 테스트 클래스를 만들었습니다. 왜 출력이 null입니까?

import java.io.ByteArrayInputStream; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlID; 
import javax.xml.bind.annotation.XmlIDREF; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlSchemaType; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

public class TestMarshall { 

    public static void main(String[] args) { 
     try { 
      // marshal(); 
      unmarshal(); 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 
    public static void marshal() throws Exception { 
     CustomerOrder co = new CustomerOrder(); 
     Agent a = new Agent(); 
     a.setUuid("sdfsdf"); 
     co.setSourceAgentUuid(a); 
     JAXBContext jc = JAXBContext.newInstance(CustomerOrder.class, 
       Agent.class); 
     Marshaller m = jc.createMarshaller(); 
     m.marshal(co, System.out); 
    } 
    public static void unmarshal() throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(CustomerOrder.class, 
       Agent.class); 
     Unmarshaller u = jc.createUnmarshaller(); 
     String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><customerOrder sourceAgentUuid=\"sdfsdf\"/>"; 
     CustomerOrder poe = (CustomerOrder) u 
       .unmarshal(new ByteArrayInputStream(testXML.getBytes())); 
     CustomerOrder co = poe; 
     System.out.println("sourceAgentUuid= " + co.getSourceAgentUuid()); 
    } 
} 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "agent") 
class Agent { 
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlID 
    @XmlSchemaType(name = "ID") 
    protected String uuid; 

    public String getUuid() { 
     return uuid; 
    } 

    public void setUuid(String uuid) { 
     this.uuid = uuid; 
    } 

} 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement 
@XmlType(name = "customerOrder") 
class CustomerOrder { 

    @XmlAttribute(name = "sourceAgentUuid") 
    @XmlIDREF 
    @XmlSchemaType(name = "IDREF") 
    protected Object sourceAgentUuid; 

    public Object getSourceAgentUuid() { 
     return sourceAgentUuid; 
    } 
    public void setSourceAgentUuid(Object sourceAgentUuid) { 
     this.sourceAgentUuid = sourceAgentUuid; 
    } 

} 
+0

정보가 충분하지 않습니다. 우리는 참조 된 "소스 에이전트 uuid"에 대한 XML 및 Java 코드, 특히 거기에 있어야하는 @XmlID를 볼 필요가 있습니다. – laune

+0

질문 끝 부분에 테스트 클래스를 추가합니다. null을 인쇄하는 이유를 도울 수 있습니까? – fivig

+0

CustomerOrder와 Agent의 두 가지 요소를 보여주는 올바른 XML 샘플이 없습니다. – laune

답변

1

@XmlIDREF 플러스의 상대 @XmlID는 는 XML의 텍스트 문자열로을 나타나는 "XMLized 포인터"입니다. JAXB는 그러한 값 쌍을 일치시키고 일치하는 참조를 멋진 오브젝트 참조로 분석합니다.

그래서, 비 정렬 화 한 후, 당신은 XML 파일에서 문자열을 볼 수 없습니다; (관리자 인 경우)

Object sourceAgentUuid 

그렇지 않은 경우는 null (그 @XmlID 그 값으로 설정과 같은 XML 요소가있는 경우)이 "원본 에이전트"개체에 대한 참조를 포함 할 것 중 하나.

+0

나는 비 정렬 화 필드 개체 ​​후 sourceAgentUuid는 XML에서 초기화 된 필드 UUID 파크 내 에이전트 클래스의 객체로 초기화했다 whant. – fivig

+0

Agent 객체의 XML 요소는 어디에 있습니까? 완전한 & 구문 적으로 올바른 XML 요소를 게시하십시오. 거기에 지금 쓰레기가 있습니다. – laune

+0

질문 끝 부분에 테스트 클래스를 추가합니다. null을 인쇄하는 이유를 도울 수 있습니까? – fivig