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 7085631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:26:54+00:00 2026-05-28T07:26:54+00:00

It would appear to be possible to unmarshal two different jaxb objects with the

  • 0

It would appear to be possible to unmarshal two different jaxb objects with the same name.

There is a Bar class …

public abstract Bar {
   private @XmlElement String val;
}

.. with Two implementations (constructors etc. left out):

@XmlRootElement(name="bar")
public class BarA extends Bar { }

@XmlRootElement(name="bar")
public class BarB extends Bar {
     private @XmlElement(required=true) String type;
}

Lastly I want to unmarshall a list of Bar documents from XML similar to the following

<bars>
  <bar>
    <val>1</val>
  </bar>
  <bar>
    <val>1</val>
    <type>2</type>
  </bar>
</bars>

The list is wrapped in an class utilizing the @XmlAnyElement

@XmlRootElement
public class Bars {

  @XmlMixed
  @XmlAnyElement(lax = true) 
  @XmlElementRefs({@XmlElementRef(BarA.class), @XmlElementRef(BarB.class)})
  private List<Bar> bars;
}

However I seem to only get instances of either BarA or BarB, whichever is the last element in the @XmlElementRefs chain.

Testing code:

String xml = ...
JAXBContext jc = JAXBContext.newInstance(Bars.class);
ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
Bars bars = (Bars) jc.createUnmarshaller().unmarshal(in);
for (Bar bar : bars.getBars()) {
    System.out.println(bar.getClass());
}

I don’t think the XmlAdapter suggested in JAXB @XmlElements, different types but same name? would neccesarily be the only approach either.

  • 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-28T07:26:55+00:00Added an answer on May 28, 2026 at 7:26 am

    Really, the first thing I would try to do is eliminate the source of the problem – having two different JAXB classes with the same name. A situation like that is bound to cause headaches, for you and for the people who have to maintain your code.

    If that’s impossible… Maybe you could write another subclass called EveryBar, and always unmarshall to that. EveryBar would contain all the fields of every other Bar subclass. Then you’d have a post-processing step on your object tree that would examine the EveryBar, and swap it for an instance of whatever type is appropriate. Yeah, it’s an ugly solution, but it’s only slightly worse than the one from your link.

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

Sidebar

Related Questions

I would like a progress bar to appear in the console window while a
Looking at the bottom of this page it would appear that it is possible
It would appear that I am suffering from a new bug in IE7, as
I am a newbie working towards developing an IE extension that would appear as
I am trying to understand how a single and multidimensional javascript array would appear
I would like to force a \marginpar to appear on the wrong side of
I would like to control which methods appear when a user uses tab-completion on
I would like to have a VM to look at how applications appear and
I would like that the Index action doesn't appear in the url. For example,
I'm currently working on some concurrent code that would appear to have a few

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.