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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:27:09+00:00 2026-06-08T11:27:09+00:00

So here is a complex/retarded situation. I am writing an XSD and there happens

  • 0

So here is a complex/retarded situation. I am writing an XSD and there happens to be a requirement where i need 2 root elements (1 at any given time)

<xs:element name="booksList">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="book" type="bookType" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence> 
    </xs:complexType>   
</xs:element>

and then

<xs:element name="book" type="bookType"></xs:element>

at any given time, either of these element will be used as the root element, so an XML would look like

<bookList>
<book>
<author>XYZ</author>
</book>
</bookList>

or

<book>
<author>XYZ</author>
</book>

Both of these XML will be sent back to the user from 2 different URL’s i.e. the list will be sent from localhost/books.xml?author=XYZ and single book will be sent from localhost/book_name.xml

How can i achieve this with one xml ? I tried putting the book definition in the XSD but JAXB2.1 didn’t generate any Book class. Is there something which i am missing ?


EDIT1: BookType has been generated but BookType doesn’t have any root element.

  • 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-08T11:27:11+00:00Added an answer on June 8, 2026 at 11:27 am

    XML SCHEMA

    I am writing an XSD and there happens to be a requirement where i need
    2 root elements (1 at any given time)

    The XML schema below supports having the two root elements booksList and book that you are looking for.

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
        <xs:element name="booksList">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="book" type="bookType" minOccurs="0"
                        maxOccurs="unbounded" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="book" type="bookType"></xs:element>
    
        <xs:complexType name="bookType">
            <xs:sequence>
                <xs:element name="author" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    
    </xs:schema>
    

    GENERATED MODEL

    I tried putting the book definition in the XSD but JAXB2.1 didn’t
    generate any Book class.

    Your JAXB (JSR-222) implementation will generate a class for the named complex type bookType, then for the bookElement it will create an @XmlElementDecl annotation on the ObjectFactory class.

    BooksList

    A class with an @XmlRootElement was generated on this class because it corresponds to a global element with an anonymous complex type.

    package forum11620825;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {"book"})
    @XmlRootElement(name = "booksList")
    public class BooksList {
    
        protected List<BookType> book;
    
        public List<BookType> getBook() {
            if (book == null) {
                book = new ArrayList<BookType>();
            }
            return this.book;
        }
    
    }
    

    BookType

    This class was generated to correspond to the named complex types.

    package forum11620825;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "bookType", propOrder = {"author"})
    public class BookType {
    
        @XmlElement(required = true)
        protected String author;
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String value) {
            this.author = value;
        }
    
    }
    

    ObjectFactory

    Global elements that correspond to named complex types have @XmlElementDecl annotations generated on the ObjectFactory class. This is necessary since multiple global elements could correspond to named complex types.

    package forum11620825;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.XmlElementDecl;
    import javax.xml.bind.annotation.XmlRegistry;
    import javax.xml.namespace.QName;
    
    @XmlRegistry
    public class ObjectFactory {
    
        private final static QName _Book_QNAME = new QName("", "book");
    
        public ObjectFactory() {
        }
    
        public BooksList createBooksList() {
            return new BooksList();
        }
    
        public BookType createBookType() {
            return new BookType();
        }
    
        @XmlElementDecl(namespace = "", name = "book")
        public JAXBElement<BookType> createBook(BookType value) {
            return new JAXBElement<BookType>(_Book_QNAME, BookType.class, null, value);
        }
    
    }
    

    XML

    Below are the XML documents from your question.

    booksList.xml

    <booksList>
        <book>
            <author>XYZ</author>
        </book>
    </booksList>
    

    book.xml

    <book>
        <author>XYZ</author>
    </book>
    

    DEMO CODE

    When you unmarshal a document in which the root element corresponds to an @XmlRootElement annotation you get an instance of the corresponding domain object. If you unmarshal a document in which the root element corresponds to an @XmlElementDecl annotation you get back an instance of JAXBElement that wraps a domain object corresponding to the named complex type.

    package forum11620825;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance("forum11620825");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
    
            File input1 = new File("src/forum11620825/booksList.xml");
            BooksList bookList = (BooksList) unmarshaller.unmarshal(input1);
    
            File input2 = new File("src/forum11620825/book.xml");
            JAXBElement<BookType> je = (JAXBElement<BookType>) unmarshaller.unmarshal(input2);
            BookType bookType = je.getValue();
        }
    
    }
    

    UPDATE

    Below is a code fragment demonstrating how to wrap an instance of BookType in a JAXBElement so that it can be marshalled.

    ObjectFactory objectFactory = new ObjectFactory();
    JAXBElement<BookType> jaxbElement = objectFactory.createBook(aBookType);
    marshaller.marshal(jaxbElement, System.out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following requirement for a very complex UI. (Complex here means there
I have a slightly complex problem here. I need to show to the client
Here's the situation: You have a very complex UI element that is repeated in
I've got this complex situation. Created EF with Model first and here are my
here is the current complex query given below. SELECT DISTINCT Evaluation.ETCode, Training.TTitle, Training.Tcomponent, Training.TImpliment_Partner,
Here's the situation: i have a SearchPage where an user can make a complex
I have a complex view, which is described here: View of multiple tables. Need
Here's the situation: I have a website with a complex address form with multiple
I have some complex types here so I decided to use nifty trick to
Here I am trying to implement a class for complex numbers using books and

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.