0

I have class

@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;
....

Also Agent class for sourceAgentUuid field

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

And unmarshalling 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="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>

When I try to unmarshall

JAXBElement poe =  (JAXBElement)u.unmarshal( new FileInputStream( "org_order.xml" ) );
CustomerOrder co=(CustomerOrder)poe.getValue();
System.out.println(co.getSourceAgentUuid());

result null (but all other not @XmlIDREF field unmarshalled ok)

How I can get ef0b03de-c95b-11e3-9183-002590a28eca result ?

I had create test class for simply. Why output is 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;
    }

}
fivig
  • 1
  • 2
  • Not enough information. We need to see the XML and Java code for the referenced "source agent uuid", especially the @XmlID that ought to be there. – laune Jun 17 '14 at 07:30
  • I add test class to the end of question. Can you help why print null? – fivig Jun 17 '14 at 09:53
  • Can't help you, still no valid XML sample showing two complete elements: CustomerOrder and Agent. – laune Jun 17 '14 at 10:02
  • Agent not element node, that is attribute of CustomerOrder – fivig Jun 17 '14 at 10:53
  • Thats valid xml String testXML = ""; – fivig Jun 17 '14 at 11:08
  • Ah, please read *carefully* what I wrote in my answer: if there is no XML element for the Agent (with @uuid="sdfsdf", the sourceAgentUuid remains null! I already wrote that - more than once! – laune Jun 17 '14 at 11:26
  • sorry, may be exist any custom way to init element for example from another source... – fivig Jun 17 '14 at 12:21
  • You'd need a mapping of the Agent ids to the CustomerOrder ids and a data set of the Agent objects, and then you could insert the references. OR, change the agent uuid to String and resolve in the getter. – laune Jun 17 '14 at 12:52
  • I can't map Agent ids to the CustomerOrder ids because any time it diferent. Also i can,t change agent uuid to String because use third party xsd. – fivig Jun 17 '14 at 15:44
  • With XmlID all work fine, but class my entity class generated with XmlIDREF. How i can generate class annotated with XmlID ? – fivig Jun 17 '14 at 16:18
  • https://jaxb.java.net/tutorial/section_4_3_1-Assembling-Data-with-Links-ID-IDREF.html#Assembling%20Data%20with%20Links%20%28ID,%20IDREF%29 – laune Jun 17 '14 at 18:33
  • The following example will help explain `@XmlID` and `@XmlIDREF`: http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html – bdoughan Jun 17 '14 at 20:04

1 Answers1

2

An @XmlIDREF plus its counterpart @XmlID are "XMLized pointers" that appear as strings in the XML text. JAXB matches such value pairs and resolves matching references into nice object references.

So, after unmarshalling, you won't see the strings from the XML file; the

Object sourceAgentUuid

(in your case) will either contain a reference to that "source agent" object (if there is such an XML element with its @XmlID set to that value) or null otherwise.

laune
  • 31,114
  • 3
  • 29
  • 42
  • I whant that after unmarshal field Object sourceAgentUuid was initialize as object of my Agent class whith initialized field Uuid from xml. – fivig Jun 17 '14 at 08:57
  • Where is the XML element for the Agent object? Please post complete & syntactically correct XML elements. What is there now is garbage. – laune Jun 17 '14 at 09:11
  • I add test class to the end of question. Can you help why print null? – fivig Jun 17 '14 at 09:53