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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:38+00:00 2026-06-13T23:30:38+00:00

Say I have two JavaBeans Person and Address . If I create a list

  • 0

Say I have two JavaBeans Person and Address.

If I create a list of Person objects, I’d like to marshal to something like this:

<persons>
  <person>...</person>
</persons>

It’s possible to use the technique described here:
Using JAXB to unmarshal/marshal a List<String>

By annotating JaxbList with @XmlRootElement(name = "persons") and @XmlElement(name = "person"), then it’s possible to marshal to the XML above.

But, it’d be nice to be able to reuse the same JaxbList<T> class to also marshal a list of Address objects. And in reality, I will have many other types of beans. I can go with something like:

<list>
   <item xsi:type="person" xmlns:xsi="http://www.w2.org/2001/XmlSchema-instance"></item>
</list>

But, ideally, it’d be nice to have it replace “list” with the plural version of class name and “item” with the class name.

So, is it possible to programmatically configure the JaxbContext or something during runtime and essentially set the value of the name inside @XmlRootElement and @XmlElement?

Or any other way to get this working without having to write a separate implementation of JaxbList for every bean type? Maybe XmlJavaTypeAdapter can achieve this sort of thing?

Update
@Blaise Doughan’s solution accepted below works great. For my use case, I needed to go straight from Java object to XML, here’s what worked (note this is not my full implementation, it’s sort of just pseudo code for demonstration):

    //JAXBContext is thread safe and so create it in constructor or 
    //setter or wherever:
    ... 
    JAXBContext jc = JAXBContext.newInstance(Wrapper.class, clazz);
    ... 

    public String marshal(List<T> things, Class clazz) {

      //configure JAXB and marshaller     
      Marshaller m = jc.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      //Create wrapper based on generic list of objects
      Wrapper<T> wrapper = new Wrapper<T>(things);
      JAXBElement<Wrapper> wrapperJAXBElement = new JAXBElement<Wrapper>(new QName(clazz.getSimpleName().toLowerCase()+"s"), Wrapper.class, wrapper);

      StringWriter result = new StringWriter();
      //marshal!
      m.marshal(wrapperJAXBElement, result);

      return result.toString();

    }
  • 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-13T23:30:40+00:00Added an answer on June 13, 2026 at 11:30 pm

    You could create a generic Wrapper object like the following:

    Wrapper

    You could create a generic wrapper class with a List property annotated with @XmlAnyElement(lax=true). The type of the object used to populate this list will be based on its root element (see: http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html).

    package forum13272288;
    
    import java.util.*;
    import javax.xml.bind.annotation.XmlAnyElement;
    
    public class Wrapper<T> {
    
        private List<T> items = new ArrayList<T>();
    
        @XmlAnyElement(lax=true)
        public List<T> getItems() {
            return items;
        }
    
    }
    

    Address

    You will need to annotate the possible contents of the list with @XmlRootElement.

    package forum13272288;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Address {
    
    }
    

    Person

    package forum13272288;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Person {
    
    }
    

    Demo

    The demo code below demonstrates how to use the Wrapper class. Since the root element can be different you will need to specify that you want to unmarshal to the wrapper class. Alternatively you could leverage the @XmlElementDecl annotation to associate multiple root elements with the wrapper class (see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html).

    package forum13272288;
    
    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Wrapper.class, Person.class, Address.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            StreamSource personsXML = new StreamSource("src/forum13272288/persons.xml");
            JAXBElement<Wrapper> wrapper1 = unmarshaller.unmarshal(personsXML, Wrapper.class);
            marshaller.marshal(wrapper1, System.out);
    
            StreamSource addressesXML = new StreamSource("src/forum13272288/addresses.xml");
            JAXBElement<Wrapper> wrapper2 = unmarshaller.unmarshal(addressesXML, Wrapper.class);
            marshaller.marshal(wrapper2, System.out);
        }
    
    }
    

    Output

    Below is the output from running the demo code. The files persons.xml and addresses.xml look just like there corresponding output.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <persons>
        <person/>
        <person/>
    </persons>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <addresses>
        <address/>
        <address/>
    </addresses>
    

    For More Information

    • http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two domain objects and a mapper interface. class Person { int
Say I have two classes like this: class A{ private static Random random =
Say I have two tables: orderinfo and customerinfo They look like this: orderinfo: OrderID
Say I have two spreadsheets. One of the spreadsheets has a table like this:
Say you have two classes, A and B. Is it possible to instantiate both
Say we have two pointers to objects that can contain a property equal and
Say we have two Integer objects: Integer i=100,j=200; Does (j-i) evaluate to another Integer
Say I have two complex nested arrays in PHP, like these: $a = array(
Say I have two lists: a = list(1,2) b = list(x,y) #a #[[1]] #[1]
Say I have two lists or arrays of strings. For example: list 1: a,

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.