I need to create the following type of XML file as a output.
JDK 1.8 (must use javax.xml.*)
Expected:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookstore xmlns:ns2="org.demo.eranga">
<bookList>
<book>
<authors>
<authorFirstName>Neil Strauss</authorFirstName>
<authorLastName>Strauss</authorLastName>
</authors>
<name>Dhamma Gaweshi</name>
<publisher>Pearson</publisher>
</book>
</bookList>
<location>Colombo Airport</location>
<name>Colombo Bookstore</name>
</ns2:bookstore>
Actual:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookstore xmlns:ns2="org.demo.eranga">
<bookList>
<book>
<authors/authorFirstName>Neil</authors/authorFirstName>
<authors/authorLastName>Strauss</authorLastName/authorId>
<isbn>978-0060554736</isbn>
<name>Dhamma Gaweshi</name>
<publisher>Pearson</publisher>
</book>
</bookList>
<location>Colombo Airport</location>
<name>Colombo Bookstore</name>
</ns2:bookstore>
XML domain class:
package org.demo.eranga;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "book")
public class Book {
@XmlElement(name="authors/authorFirstName")
private String authorFirstName;
@XmlElement(name="authors/authorLastName")
private String authorLastName;
private String publisher;
private String isbn;
private String name;
public void setAuthorFirstName(String authorFirstName) {
this.authorFirstName= authorFirstName;
}
Is there a way to create an XML sub-element based on the @XmlElement name
like:
@XmlElement(name=”authors/authorName”)
private String authorName;
The demo project can be found here in the git hub