簡體   English   中英

jaxb解組子級@XmlIDREF

[英]jaxb unmarshall child @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;
....

也是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片段

<?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="it@erms" name="440" updated="2014-04-22T12:19:56.472+04:00" updatedBy="it@erms" 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());

結果為null(但所有其他非@XmlIDREF字段均已編組​​好)

如何獲得ef0b03de-c95b-11e3-9183-002590a28eca結果?

我已經簡單地創建了測試類。 為什么輸出為空?

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;
    }

}

@XmlIDREF及其對應的@XmlID是“ XML指針”,在XML文本中顯示為字符串。 JAXB匹配這些值對,並將匹配的引用解析為漂亮的對象引用。

因此,在解組后,您將看不到XML文件中的字符串。

Object sourceAgentUuid

(在您的情況下)將包含對該“源代理”對象的引用(如果存在這樣一個XML元素,其@XmlID設置為該值),否則將為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM