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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:02+00:00 2026-06-06T05:12:02+00:00

It seems JAXB can’t read what it writes. Consider the following code: interface IFoo

  • 0

It seems JAXB can’t read what it writes. Consider the following code:

interface IFoo {
    void jump();
}

@XmlRootElement
class Bar implements IFoo {
    @XmlElement
    public String y;

    public Bar() {
        y = "";
    }

    public Bar(String y) {
        this.y = y;
    }

    @Override
    public void jump() {
        System.out.println(y);
    }
}

@XmlRootElement
class Baz implements IFoo {
    @XmlElement
    public int x;

    public Baz() {
        x = 0;
    }

    public Baz(int x) {
        this.x = x;
    }

    @Override
    public void jump() {
        System.out.println(x);
    }
}

@XmlRootElement
public class Holder {
    private List<IFoo> things;

    public Holder() {
        things = new ArrayList<>();
    }

    @XmlElementWrapper
    @XmlAnyElement
    public List<IFoo> getThings() {
        return things;
    }

    public void addThing(IFoo thing) {
        things.add(thing);
    }
}

// ...

try {
    JAXBContext context = JAXBContext.newInstance(Holder.class, Bar.class, Baz.class);

    Holder holder = new Holder();
    holder.addThing(new Bar("1"));
    holder.addThing(new Baz(2));
    holder.addThing(new Baz(3));

    for (IFoo thing : holder.getThings()) {
        thing.jump();
    }

    StringWriter s = new StringWriter();
    context.createMarshaller().marshal(holder, s);

    String data = s.toString();

    System.out.println(data);

    StringReader t = new StringReader(data);
    Holder holder2 = (Holder)context.createUnmarshaller().unmarshal(t);

    for (IFoo thing : holder2.getThings()) {
        thing.jump();
    }
}
catch (Exception e) {
    System.err.println(e.getMessage());
}

It’s a simplified example, of course. The point is that I have to store two very differently implemented classes, Bar and Baz, in one collection. Well, I observed that they have pretty similar public interface, so I created an interface IFoo and made them two to implement it. Now, I want to have tools to save and load this collection to/from XML. Unfortunately, this code doesn’t quite work: the collection is saved, but then it cannot be loaded! The intended output is

1
2
3
some xml
1
2
3

But unfortunately, the actual output is

1
2
3
some xml
com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to testapplication1.IFoo

Apparently, I need to use the annotations in a different way? Or to give up on JAXB and look for something else? I, well, can write “XMLNode toXML()” method for all classes I wan’t to (de)marshal, but…

  • 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-06T05:12:03+00:00Added an answer on June 6, 2026 at 5:12 am

    Try the following @XmlAnyElement(lax=true). The lax flag tells the JAXB (JSR-222) implementation to match elements to domain objects based their @XmlRootElement and @XmlElementDecl annotations. Without it the contents are treated as DOM nodes.

    @XmlRootElement
    public class Holder {
        private List<IFoo> things;
    
        public Holder() {
            things = new ArrayList<>();
        }
    
        @XmlElementWrapper
        @XmlAnyElement(lax=true)
        public List<IFoo> getThings() {
            return things;
        }
    
        public void addThing(IFoo thing) {
            things.add(thing);
        }
    }
    

    For More Information

    • http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get jaxb to bind to my Vector? I cannot seem to
I'm rather new to JAXB and have come across a problem which I can't
I have defined my bindings in the following way <jaxb:bindings node=xs:complexType[@name='Parent']> ........... <jaxb:bindings node=xs:element[@name='children']>
I have a class like this: @XmlRootElement(name = PricingGroup) public class PricingGroup { ...
It seems like this should be fairly obvious, but I can't find anything on
JAXB works fine for marshalling and unmarshalling. Because it can be slow to create
I am trying to unmarshall the following XML using JAXB: <Works> <Work> <Composers> <Composer>
I'm using JAXB with Scala, my marshalling code looks like this: def marshalToXml(): String
Apparently in version 2 of JAXB - the validator class has been deprecated -
I work with a datamodel created using JAXB, from that I can generate XML

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.