Is it possible to handle forward references of XML IDREF elements in JAXB XmlAdapter during the unmarshal process? For example, I have the following XML complexType:
<xs:complexType name="person">
<xs:complexContent>
<xs:sequence>
<xs:element name="dateOfBirth" type="xs:dateTime" minOccurs="0"/>
<xs:element name="firstName" type="xs:string" minOccurs="0"/>
<xs:element name="gender" type="xs:string" minOccurs="0"/>
<xs:element name="guardian" type="xs:IDREF" minOccurs="0"/>
<xs:element name="homePhone" type="xs:string" minOccurs="0"/>
<xs:element name="lastName" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexContent>
</xs:complexType>
where the guardian field could reference another Person-type element elsewhere in the document. I am currently using an XmlAdapter when marshalling so that the first time an object is marshalled, it is marshalled by containment, and any subsequent occurances of this object are marshalled by reference. See a previous question of mine. However, due to how my XML instance documents are created, the first occurrence of a Person element could happen after an IDREF to it occurs.
Is this something that is possible? Or do I need to approach this differently? Thanks!
I have an answer to your related question I outlined how an
XmlAdaptercould be used to implement the use case where the first occurrence of an object was marshalled via containment/nesting and all other occurrences were marshalled by reference:Option #1 –
@XmlID/@XmlIDREFIf all of your
Personobjects are all represented through nesting and you want to introduce some key based relationships then you are best of using@XmlIDto mark a field/property as the key, and@XmlIDto map a field/property as a foreign key. YourPersonclass would look something like:For More Information
Option #2 – Using
XmlAdapterIf you updated the
XmlAdapterfrom my previous answer to be:Then you will be able to unmarshal XML documents that look like the following where the reference happens first: