Java jaxb解组子项@XmlIDREF

Java jaxb解组子项@XmlIDREF,java,xml,jaxb,idref,Java,Xml,Jaxb,Idref,我有课 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "customerOrder", propOrder = { "demandsUuid", "invoicesOutUuid", "paymentsUuid", "customerOrderPosition", "purchaseOrdersUuid" }) public class CustomerOrder extends Legen

我有课

@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;
....
还包括SourceAgentUID字段的代理类

@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 Snippets

<?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());
结果为空(但所有其他非@XmlIDREF字段解组ok)

如何获得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;
    }

}
import java.io.ByteArrayInputStream;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.XmlID;
导入javax.xml.bind.annotation.XmlIDREF;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.XmlSchemaType;
导入javax.xml.bind.annotation.XmlType;
导入javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
导入javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
公共类测试{
公共静态void main(字符串[]args){
试一试{
//元帅();
解组();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void封送处理()引发异常{
CustomerOrder co=新CustomerOrder();
代理a=新代理();
a、 setUuid(“sdfsdf”);
co.setSourceAgentUuid(a);
JAXBContext jc=JAXBContext.newInstance(CustomerOrder.class,
代理类);
Marshaller m=jc.createMarshaller();
m、 元帅(co,系统输出);
}
public static void unmarshal()引发异常{
JAXBContext jc=JAXBContext.newInstance(CustomerOrder.class,
代理类);
解组器u=jc.createUnmarshaller();
字符串testXML=“”;
CustomerOrder poe=(CustomerOrder)u
.unmarshal(新的ByteArrayInputStream(testXML.getBytes());
客户订单co=poe;
System.out.println(“sourceagentuid=“+co.getsourceagentuid());
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name=“agent”)
类代理{
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
@XmlSchemaType(name=“ID”)
受保护字符串uuid;
公共字符串getUuid(){
返回uuid;
}
公共无效setUuid(字符串uuid){
this.uuid=uuid;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name=“customerOrder”)
类客户订单{
@XmlAttribute(name=“sourceagentuid”)
@XmlIDREF
@XmlSchemaType(name=“IDREF”)
受保护对象SourceAgentUID;
公共对象getSourceAgentUID(){
返回sourceagentuid;
}
public void setSourceAgentUuid(对象sourceAgentUuid){
this.sourceagentuid=sourceagentuid;
}
}

一个@XmlIDREF及其对应的@XmlID是“XML化指针”,在XML文本中显示为字符串。JAXB匹配这些值对,并将匹配的引用解析为漂亮的对象引用

因此,在解组后,您将看不到XML文件中的字符串;

Object sourceAgentUuid

(在您的情况下)将包含对该“源代理”对象的引用(如果存在将@XmlID设置为该值的XML元素),否则将为null。

信息不足。我们需要查看引用的“源代理uuid”的XML和Java代码,特别是应该存在的@XmlID。你能帮我解释为什么打印null吗?帮不了你,仍然没有显示两个完整元素的有效XML示例:CustomerOrder和Agent。Agent不是元素节点,这是CustomerOrderThats的属性有效的XML字符串testXML=“”;在解组字段对象SourceAgentUID之后,我发现它被初始化为我的代理类的对象,其中初始化了来自xml的字段Uuid。代理对象的xml元素在哪里?请发布完整且语法正确的XML元素。现在存在的是垃圾。我在问题的末尾添加了测试类。你能帮我解释为什么打印空吗?