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

  • Home
  • SEARCH
  • 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 7786635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:26:57+00:00 2026-06-01T20:26:57+00:00

update – see edit at the bottom IDRefs/keyrefs seem to be possible in JAXB

  • 0

update – see edit at the bottom

IDRefs/keyrefs seem to be possible in JAXB annotations, but the ref ends up being element text.

I would like the ref to be an attribute of an element.

For example, given this object model:

@XmlType
public class Employee {
    @XmlID
    @XmlAttribute
    String name;
    @XmlAttribute
    int years;
    @XmlAttribute
    String foo;
}

@XmlType
public class Office {
    @XmlAttribute
    String name;
    @XmlElementWrapper
    @XmlElement(name = "employee")
    List<Employee> employees;
}

@XmlRootElement
public class Company {
    @XmlElementWrapper
    @XmlElement(name = "office")
    List<Office> offices;
    @XmlElementWrapper
    @XmlElement(name = "employee")
    List<Employee> employees;
}

I would like the externalized xml format to end up looking like this:

<company>
    <offices>
        <office name="nyc">
            <employees>
                <!--*** id ref to employee name ***-->
                <employee ref="alice"/>
                <employee ref="bob"/>
            </employees>
        </office>
        <office name="sf">
            <employees>
                <employee ref="connie"/>
                <employee ref="daphne"/>
            </employees>
        </office>
    </offices>
    <employees>
        <!-- *** name is the id *** -->
        <employee name="alice" years="3" foo="bar"/>
        <employee name="bob" years="3" foo="bar"/>
        <employee name="connie" years="3" foo="bar"/>
        <employee name="daphne" years="3" foo="bar"/>
    </employees>
</company>

Instead, the best I can do is this (with the annotations listed above in the java code):

<company>
    <offices>
        <office name="nyc">
            <employees>
                <employee>alice</employee>
                <employee>bob</employee>
            </employees>
        </office>
        <office name="sf">
            <employees>
                <employee>connie</employee>
                <employee>daphne</employee>
            </employees>
        </office>
    </offices>
    <employees>
        <employee name="alice" years="3" foo="bar"/>
        <employee name="bob" years="3" foo="bar"/>
        <employee name="connie" years="3" foo="bar"/>
        <employee name="daphne" years="3" foo="bar"/>
    </employees>
</company>

Is there a way I can force the idref value to be an attribute of employee, rather than element body text? I know I can do this with an XML Schema, but I’d like to stick to annotations if at all possible.

Thank you.

Edit The solution by Torious below almost works, but it doesn’t quite work under some circumstances.

unmarshalling fails if the “offices” elements come before (in the xml file) the “employee” elements that the office references. The employee references are not found, and the EmployeeRef wrapper has a null employee object. If the “employee” are first, it works.

This wouldn’t be so much of a problem, but the marshal method will put “offices” first, so that trying to unmarshal what has just been marshalled fails.

Edit 2 comment in Torious’ answer solves the ordering problem.

  • 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-06-01T20:26:58+00:00Added an answer on June 1, 2026 at 8:26 pm

    The solution is to use an XmlAdapter which wraps an Employee in an instance of a new type, EmployeeRef, which specifies how to map the XML id ref:

    @XmlType
    public class Office {
    
        @XmlAttribute
        String name;
    
        @XmlElementWrapper
        @XmlElement(name="employee")
        @XmlJavaTypeAdapter(EmployeeAdapter.class) // (un)wraps Employee
        List<Employee> employees;
    }
    
    @XmlType
    public class EmployeeRef {
    
        @XmlIDREF
        @XmlAttribute(name="ref")
        Employee employee;
    
        public EmployeeRef() {
        }
    
        public EmployeeRef(Employee employee) {
            this.employee = employee;
        }
    }
    
    public class EmployeeAdapter extends XmlAdapter<EmployeeRef, Employee> {
    
        @Override
        public EmployeeRef marshal(Employee employee) throws Exception {
            return new EmployeeRef(employee);
        }
    
        @Override
        public Employee unmarshal(EmployeeRef ref) throws Exception {
            return ref.employee;
        }
    }
    

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE - I'm probably being daft, see my last update below. I just did
(update) ICustomTypeDescriptor works for my Windows Forms app, but not for Silverlight; Not supported.
Update: This does work, I was being stupid :( i have the following extension
Update: This is, as I was told, no principle Python related problem, but seems
UPDATE: Solution at bottom. I recently switched from using a set up such as
UPDATE: Thanks for the answers! I can't seem to figure it out, even with
[UPDATE] Was just an idiot mistake. See end for solution. I am trying to
* UPDATE: *I've already answered my question. But you can still give me advise
UPDATE : Answer at the bottom. Hi Guys, How to initialize an 'array of
UPDATE: Please see https://softwarerecs.stackexchange.com/questions/71464/java-library-to-insert-invisible-text-into-a-pdf instead. I want to insert invisible text into an existing

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.