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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:58:27+00:00 2026-06-09T00:58:27+00:00

I currently have this environment on my project: public abstract class Foo { private

  • 0

I currently have this environment on my project:

public abstract class Foo {

   private List<Thing> things;

   public List<Thing> getThings() { return this.things; }
}

public abstract class Bar extends Foo {


   @XmlElements({@XmlElement(name = "first", type = First.class)})
   public List<Thing> getThings() { return super.getThings(); }

}

public class Bobar extends Bar {

   @XmlElements({@XmlElement(name = "second", type = Second.class)})
   public List<Thing> getThings() { return super.getThings(); }

}

For the following XML document

<bobar>
   <first>blablabla</first>
   <second>blublublu</second>
</bobar>

When I do

context = JAXBContext.newInstance("the.package.structure");
unmarshaller = context.createUnmarshaller();
Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document");

The Bar object only has one element in the collection, not 2. The First element is completly lost, when I try to do object.getThings(), its size is 1 and the only object inside the collection is an instance of Second. Can someone help me how can I achieve to get both objects in the collection? And if that’s not possible, how can I achieve something similar to this?

The reason I’m doing this is that (in my project logic) every Bobars things collection has a First in its collection, but not every Bar has a Second in its collection, and Foo is a generic class.

Edit:

When I change the order in my XML document, the output is different.

<bobar>
   <second>blablabla</second>
   <first>blublublu</first>
</bobar>

In this scenario, I get only an instance of First in the collection, and Second is lost. And changing the scenario more, I get interesting results:

public abstract class Foo {

   private List<Thing> things;

   public List<Thing> getThings() { return this.things; }
}

public abstract class Bar extends Foo {


   @XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)})
   public List<Thing> getThings() { return super.getThings(); }

}

public class Bobar extends Bar {

   @XmlElements({@XmlElement(name = "second", type = Second.class)})
   public List<Thing> getThings() { return super.getThings(); }

}

If I do

<bobar>
   <third>bliblibli</third>
   <second>blablabla</second>
   <first>blublublu</first>
</bobar>

In theory, I think this shouldn’t be validated against the XML Schema generated by that, as the order here is not correct. But besides that, in such scenario, I get Second and First, the Third is lost.

  • 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-09T00:58:29+00:00Added an answer on June 9, 2026 at 12:58 am

    It is not possible to annotate a property on a super type, and have a sub try incrementally add to that mapping. Below is a way you could support all the use cases that you are after. One thing to be cautious of is that all levels in the object hierarchy would support the same set of elements. You would need to use a external means of validation to restrict the desired values.

    If Thing is a class not an interface, and First and Second extend Thing then you may be interested in using @XmlElementRef instead of @XmlElements (see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html). It will offer you more flexibility, at the cost of some validation (hard to restrict the set of valid values).

    Bar

    We will annotate the Bar with @XmlTransient so that the JAXB implementation doesn’t process it.

    package forum11698160;
    
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlTransient
    public abstract class Bar extends Foo {
    
        public List<Thing> getThings() {
            return super.getThings();
        }
    
    }
    

    Bobar

    @XmlElementRef corresponds to the concept of substitution groups in XML schema. The values matching the property will be based on @XmlRootElement declarations.

    package forum11698160;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Bobar extends Bar {
    
        @XmlElementRef
        public List<Thing> getThings() {
            return super.getThings();
        }
    
    }
    

    Thing

    As JAXB implementations can not use reflection to find all the subclasses of a type, we can use the @XmlSeeAlso annotation to help out. If you don’t use this annotation then you will need to include all the subtypes when bootstrapping the JAXBContext.

    package forum11698160;
    
    import javax.xml.bind.annotation.XmlSeeAlso;
    
    @XmlSeeAlso({First.class, Second.class})
    public class Thing {
    
    }
    

    First

    We will need to annotate First with @XmlRootElement:

    package forum11698160;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class First extends Thing {
    
    }
    

    Second

    Second will also need to be annotated with @XmlRootElement:

    package forum11698160;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Second extends Thing {
    
    }
    

    Demo

    package forum11698160;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Bobar.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum11698160/input.xml");
            Bobar bobar =  (Bobar) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(bobar, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <bobar>
        <first/>
        <second/>
    </bobar>
    

    OTHER FILES

    Below are the other files you need to run this example:

    Foo

    package forum11698160;
    
    import java.util.*;
    
    public abstract class Foo {
    
        private List<Thing> things = new ArrayList<Thing>();
    
        public List<Thing> getThings() {
            return this.things;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this code : Private Sub Worksheet_Change(ByVal Target As Range) Dim lastrow
I currently have this code Private Sub Worksheet_Change(ByVal Target As Range) WorksheetChanged(Target, Range(AB3).CurrentRegion, Range(B18:B19))
I currently have a web project developed with Codeigniter. My production environment works as
I have this setup currently: Project A outputs a war file - has a
I currently have this sql statement that I wrote and it works but it's
I currently have this and it works fine but I wanted to have a
I currently have this: function submit() { document.getElementById(lostpasswordform).click(); // Simulates button click document.lostpasswordform.submit(); //
I currently have this code which stores XML into an XML-type column called data,
I currently have this set up and working fine inside a users folder. RewriteEngine
I currently have this code: PACKETS = {}; function AddPacket(data) local id = data.ID;

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.