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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:05:56+00:00 2026-05-27T03:05:56+00:00

I have the following: class A{ @XmlElement String name; //getters and setters } and

  • 0

I have the following:

class A{
    @XmlElement
    String name;

    //getters and setters
}

and

class B extends A{
    @XmlElement
    String height;

    //getters and setters
}

finally I have

@XmlRootElement
class P{
    @XmlElement
    List<A> things;

    //getters and setters
}

If I do

List<A> l = new ArrayList<A>();
l.add(new B('hello', 20)) //Add new B with height of 20 and name hello

P p = new P();
p.setThings(l); //Set things to list of B's.

and marshal P, I only get the field as part of things and not height.

I know that I can add @XmlSeeAlso(B.class) in A and it will all work.

But the issue is that I don’t know all extended classes other than B, as A may be extended on runtime.

How do I dynamically define @XmlSeeAlso on runtime?

  • 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-27T03:05:56+00:00Added an answer on May 27, 2026 at 3:05 am

    This depends on how you are creating your JAXBContext. The newInstance method can be called with an explicit list of all your classes, the documentation for that method also gives a similar example.

    The client application must supply a list of classes that the new context object needs to recognize. Not only the new context will recognize all the classes specified, but it will also recognize any classes that are directly/indirectly referenced statically from the specified classes. Subclasses of referenced classes nor @XmlTransient referenced classes are not registered with JAXBContext. For example, in the following Java code, if you do newInstance(Foo.class), the newly created JAXBContext will recognize both Foo and Bar, but not Zot or FooBar:

    class Foo {
        @XmlTransient FooBar c;
        Bar b;
    }
    class Bar { int x; }
    class Zot extends Bar { int y; }
    class FooBar { }
    

    Edit: If you know at least the package names of potential jaxb classes you could also create a context given a context path.

    If the above is not possible you could also create the list of classes at runtime, based on the object you want to serialize. I think it would be better to try to refactor your code to make this unnecessary. The code below is untested:

    Set<Class> classes = new HashSet<Class>();
    classes.add(p.getClass());
    for (A a : p.getThings()) {
        classes.add(a.getClass());
    }
    JAXBContext context = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
i have a class like class CXmlData { String Data1=; String Data2=; List<String> lst=new
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]
I have the following class public class Car { public Name {get; set;} }
I have the following class: public abstract class AbstractParent { static String method() {
I have a simple class derived from a generic list of string as follows:
We have the followig code: [Serializable] public class Class1 { [XmlElement(description)] public string Description
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <strategies> <strategy name=God Class>
I have the following class and data members (plus irrelevant methods) I am new
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int

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.