Search code examples
xmljaxbannotationsxmlnode

JAXB annotation for childnode attribute


Lets say that i have following XML and i use JAXB for marshalling:

<bookstore name="bookstoreName">
    <book title="bookTitle">
         <author fullname="authorName">
         </author>
    </book>
</bookstore>

I know how to create:

  • root node
  • attributes of root node
  • child nodes

I did this by using annotations @XmlRootElement, @XmlElement and @XmlAttribute.

What i don't know is how to create attributes of child nodes. For example attribute "title".


Solution

  • title will be a property on the Book class annotated with @XmlAttribute. Book will be referenced by BookStore probably as a List property.

    @XmlElement(name="book")
    List<Book> getBooks() {
        return books;
    }