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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:01:44+00:00 2026-06-11T00:01:44+00:00

I’m trying to marshall multiple objects e.g. Book added into BookLists via setBookslst() .

  • 0

I’m trying to marshall multiple objects e.g. Book added into BookLists via setBookslst(). I begin using this JAXBContext setup:

jaxbContext = JAXBContext.newInstance(BookLists.class);

and

 jaxbMarshaller.marshal(lists, result);

I’m given the following runtime exception however:

javax.xml.bind.JAXBException: com.jaxb.example.marshall.Book nor any
of its super class is known to this context]

My types are defined as follows.

Book :-

@XmlRootElement(name="book")
public class Book {

     private String title;
     private int year;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
}

BookList :-

@XmlRootElement(name="lists")
public class BookLists {
List<Book> bookslst;

public List getBookslst() {
    return bookslst;
}

public void setBookslst(List bookslst) {
    this.bookslst = bookslst;
}

}

Marshall Code:-

Book book;
    BookLists lists=new BookLists();
    List lst=new ArrayList();
    book = new Book();
    book.setTitle("Book title");
    book.setYear(2010);
    lst.add(book);
    book = new Book();
    book.setTitle("Book title1");
    book.setYear(2011);
    lst.add(book);
    lists.setBookslst(lst);
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(BookLists.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter result = new StringWriter();

        jaxbMarshaller.marshal(lists, result);
        String xml = result.toString();
        System.out.println(xml);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I am trying to put @XMLSeeAlso annotations(Ref:- JAXB Exception: Class not known to this context). This annotation is not available in my version.

  • 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-11T00:01:46+00:00Added an answer on June 11, 2026 at 12:01 am

    By default a JAXB (JSR-222) implementation examines the public accessor methods. You could add the Book parameter on the List in your get/set methods.

    public List<Book> getBookslst() {
        return bookslst;
    }
    
    public void setBookslst(List<Book> bookslst) {
        this.bookslst = bookslst;
    }
    

    Alternatively you could specify the type of the property using the @XmlElement annotation:

    @XmlElement(type=Book.class)
    public List getBookslst() {
        return bookslst;
    }
    

    You could also specify that your JAXB implementation introspect the fields instead of the properties:

    @XmlRootElement(name="lists")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class BookLists {
        List<Book> bookslst;
    }
    

    UPDATE

    Is there any alternative way to add List instead of BookList in
    Marshallar.Marshall?

    You could create a generic List wrapper object that leveraged the @XmlAnyElement(lax=true) annotation (see: http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html). Then it cold handle a List of anything annotated with @XmlRootElement.

    Lists

    package forum12323397;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Lists<VALUE> {
    
        private List<VALUE> values = new ArrayList<VALUE>();
    
        @XmlAnyElement(lax=true)
        public List<VALUE> getValues() {
            return values;
        }
    
    }
    

    Demo

    package forum12323397;
    
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Lists.class, Book.class);
    
            Lists<Book> lists = new Lists<Book>();
    
            Book book1 = new Book();
            book1.setTitle("A Book");
            book1.setYear(2001);
            lists.getValues().add(book1);
    
            Book book2 = new Book();
            book2.setTitle("Another Book");
            book2.setYear(2007);
            lists.getValues().add(book2);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(lists, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <lists>
        <book>
            <title>A Book</title>
            <year>2001</year>
        </book>
        <book>
            <title>Another Book</title>
            <year>2007</year>
        </book>
    </lists>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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 new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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.