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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:18:27+00:00 2026-05-30T22:18:27+00:00

I’m trying to use JAXB to unmarshall some XML, but I’m getting an Unable

  • 0

I’m trying to use JAXB to unmarshall some XML, but I’m getting an “Unable to create an instance of…” exception. I understand why–it’s trying to make an instance of an abstract class. What I want is to have it make an instance of a specific implementing class. My goal with this is to have class-specific checks on setter methods. Maybe “qux” is a valid baz value for BarImpl, but BarImpl2 wants to do something else.

I got part of the way there by not annotating Foo, but if I unannotate bar, things get ugly.

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.junit.Test;


public class JAXBTest {

    @Test
    public void test() throws javax.xml.bind.JAXBException {
        String xml = 
            "<foo>" +
            "  <bar>" +
            "    <baz>qux</baz>" +
            "  </bar>" +
            "</foo>";

        javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(
                FooImpl.class,
                BarImpl.class
        );

        javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller();

        unmarshaller.unmarshal(new java.io.StringReader(xml));
    }

    @XmlRootElement(name="foo")
    public static abstract class Foo {
        @XmlElement(name="bar")
        Bar bar;
    }

    @XmlRootElement(name="bar")
    public static abstract class Bar {
        @XmlElement(name="baz")
        String baz;
    }

    public static class FooImpl extends Foo { }
    public static class BarImpl extends Bar { }
}
  • 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-30T22:18:29+00:00Added an answer on May 30, 2026 at 10:18 pm

    You could do the following:

    • Annotation the impl classes with @XmlRootElement instead of the abstract classes.
    • Mark the abstract classes with @XmlTransient (see http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html)
    • Use @XmlElement(type=BarImpl.class) on the bar property to specify the concrete type (see http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html).

    JAXBTest

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElements;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    import org.junit.Test;
    
    
    public class JAXBTest {
    
        @Test
        public void test() throws javax.xml.bind.JAXBException {
            String xml = 
                "<foo>" +
                "  <bar>" +
                "    <baz>qux</baz>" +
                "  </bar>" +
                "</foo>";
    
            javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(
                    FooImpl.class,
                    BarImpl.class
            );
    
            javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller();
    
            unmarshaller.unmarshal(new java.io.StringReader(xml));
        }
    
        @XmlTransient
        public static abstract class Foo {
            @XmlElements({
                @XmlElement(name="bar",type=BarImpl.class),
                @XmlElement(name="bar",type=BarImpl2.class),
            })
            Bar bar;
        }
    
        @XmlTransient
        public static abstract class Bar {
            @XmlElement(name="baz")
            String baz;
        }
    
        @XmlRootElement(name="foo")
        public static class FooImpl extends Foo { }
    
        @XmlRootElement(name="bar")
        public static class BarImpl extends Bar { }
    
        public static class BarImpl2 extends Bar { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.