Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6674603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:45:55+00:00 2026-05-26T03:45:55+00:00

Is it possible to handle forward references of XML IDREF elements in JAXB XmlAdapter

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:45:55+00:00Added an answer on May 26, 2026 at 3:45 am

    I have an answer to your related question I outlined how an XmlAdapter could 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:

    • Can JAXB marshal by containment at first then marshal by @XmlIDREF for subsequent references?

    Option #1 – @XmlID/@XmlIDREF

    If all of your Person objects are all represented through nesting and you want to introduce some key based relationships then you are best of using @XmlID to mark a field/property as the key, and @XmlID to map a field/property as a foreign key. Your Person class would look something like:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Person {
    
        @XmlID
        private String id;
    
        @XmlIDREF
        private Person guardian;
    }
    

    For More Information

    • http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

    Option #2 – Using XmlAdapter

    If you updated the XmlAdapter from my previous answer to be:

    package forum7587095;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlSeeAlso;
    import javax.xml.bind.annotation.XmlType;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class PhoneNumberAdapter extends XmlAdapter<PhoneNumberAdapter.AdaptedPhoneNumber, PhoneNumber>{
    
        private List<PhoneNumber> phoneNumberList = new ArrayList<PhoneNumber>();
        private Map<String, PhoneNumber> phoneNumberMap = new HashMap<String, PhoneNumber>();
    
        @XmlSeeAlso(AdaptedWorkPhoneNumber.class)
        @XmlType(name="phone-number")
        public static class AdaptedPhoneNumber {
            @XmlAttribute public String id;
            public String number;
    
            public AdaptedPhoneNumber() {
            }
    
            public AdaptedPhoneNumber(PhoneNumber phoneNumber) {
                id = phoneNumber.getId();
                number = phoneNumber.getNumber();
            }
    
            public PhoneNumber getPhoneNumber() {
                PhoneNumber phoneNumber = new PhoneNumber();
                phoneNumber.setId(id);
                phoneNumber.setNumber(number);
                return phoneNumber;
            }
    
        }
    
        @XmlType(name="work-phone-number")
        public static class AdaptedWorkPhoneNumber extends AdaptedPhoneNumber {
    
            public String extension;
    
            public AdaptedWorkPhoneNumber() {
            }
    
            public AdaptedWorkPhoneNumber(WorkPhoneNumber workPhoneNumber) {
                super(workPhoneNumber);
                extension = workPhoneNumber.getExtension();
            }
    
            @Override
            public WorkPhoneNumber getPhoneNumber() {
                WorkPhoneNumber phoneNumber = new WorkPhoneNumber();
                phoneNumber.setId(id);
                phoneNumber.setNumber(number);
                phoneNumber.setExtension(extension);
                return phoneNumber;
            }
    }
    
        @Override
        public AdaptedPhoneNumber marshal(PhoneNumber phoneNumber) throws Exception {
            AdaptedPhoneNumber adaptedPhoneNumber;
            if(phoneNumberList.contains(phoneNumber)) {
                if(phoneNumber instanceof WorkPhoneNumber) {
                    adaptedPhoneNumber = new AdaptedWorkPhoneNumber();
                } else {
                    adaptedPhoneNumber = new AdaptedPhoneNumber();
                }
                adaptedPhoneNumber.id = phoneNumber.getId();
            } else {
                if(phoneNumber instanceof WorkPhoneNumber) {
                    adaptedPhoneNumber = new AdaptedWorkPhoneNumber((WorkPhoneNumber)phoneNumber);
                } else {
                    adaptedPhoneNumber = new AdaptedPhoneNumber(phoneNumber);
                }
                phoneNumberList.add(phoneNumber);
            }
            return adaptedPhoneNumber;
        }
    
        @Override
        public PhoneNumber unmarshal(AdaptedPhoneNumber adaptedPhoneNumber) throws Exception {
            PhoneNumber phoneNumber = phoneNumberMap.get(adaptedPhoneNumber.id);
            if(null != phoneNumber) {
                if(adaptedPhoneNumber.number != null) {
                    phoneNumber.setNumber(adaptedPhoneNumber.number);
                }
                return phoneNumber;
            }
            phoneNumber = adaptedPhoneNumber.getPhoneNumber();
            phoneNumberMap.put(phoneNumber.getId(), phoneNumber);
            return phoneNumber;
        }
    
    }
    

    Then you will be able to unmarshal XML documents that look like the following where the reference happens first:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer>
        <phone-number id="A"/>
        <phone-number id="B">
            <number>555-BBBB</number>
        </phone-number>
        <phone-number id="A">
            <number>555-AAAA</number>
        </phone-number>
        <phone-number xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="work-phone-number" id="W">
            <number>555-WORK</number>
            <extension>1234</extension>
        </phone-number>
        <phone-number xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="work-phone-number" id="W"/>
    </customer>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is that possible to have a single PHP SOAP server which will handle requests
Is it possible to have a draggable element with more than one handle? I
is it possible to handle two data sources in the UIPickerView? I have here
In Struts 1 you could have, in struts-config.xml, a declaration like: <action path=/first forward=/second.do>
Is it possible to handle POSIX signals within the Java Virtual Machine? At least
Is it possible to handle this event in some way? What happens in terms
Is it possible to handle such events as: Ctrl + mouse left button click;
Is it possible to handle a mouseDown event in VB.Net (2008) regardless of the
I'm trying to handle this possible exploit and wondering what is the best way
I'm trying to create a good way to handle all possible collisions between two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.