I have the following xml format that i want to bind it through a POJO and using JAXB annotations. The XML format is the following:
<datas>
<data>apple<data>
<data>banana<data>
<data>orange<data>
<datas>
And i'm trying to bind the data through the following POJO:
#XmlRootElement()
#XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
#XmlElement
private List<String> data;
//get/set methods
}
And also i try and this POJO:
#XmlRootElement()
#XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
#XmlElement
private List<Data> datas;
//get/set methods
}
//
#XmlRootElement()
#XmlAccessorType(XmlAccessType.FIELD)
public class Data{
#XmlElement
private String data;
//get/set methods
}
In the first case it retrieves only the first data: apple. In the second case doesn't retrieve anything. Could someone help me to provide the appropriate POJO and annotations in order to bind all data?
You can do one of the following options:
OPTION #1
Datas
package forum11311374;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
private List<String> data;
//get/set methods
}
For More Information
http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
OPTION #2
Datas
package forum11311374;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
#XmlElement(name="data")
private List<Data> datas;
//get/set methods
}
Data
package forum11311374;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class Data{
#XmlValue
private String data;
//get/set methods
}
For More Information
http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
The following can be used with both options:
input.xml/Ouput
I have updated the XML document to contain the necessary closing tags. <data>apple</data> instead of <data>apple<data>.
<datas>
<data>apple</data>
<data>banana</data>
<data>orange</data>
</datas>
Demo
package forum11311374;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Datas.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11311374/input.xml");
Datas datas = (Datas) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(datas, System.out);
}
}
The first option did work for me... not sure why you are getting the problem...
Try this annotation...
#XmlElements(#XmlElement(name="data", type=String.class))
private List<String> datas; //ignore the variable name
Related
I would like that my ParentClass has final fields, 'brokenChildList' list is wrapped xml element and list items have different tag than the list (<brokenChildList><brokenChild/></brokenChildList>).
Here is a snippet of code to reproduce my issues (imports are partially truncated, setters and getters omitted)
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class Main {
public static void main(String... args) throws IOException {
ObjectMapper xmlMapper = new XmlMapper();
String xmlString = "<ParentClass><childClass name=\"name1\" value=\"val1\"/><brokenChildList><brokenChild name=\"bc1\" reason=\"bc-val1\"/><brokenChild name=\"bc2\" reason=\"bc-val2\"/></brokenChildList></ParentClass>";
ParentClass parentClass = xmlMapper.readValue(xmlString, ParentClass.class);
StringWriter stringWriter = new StringWriter();
xmlMapper.writeValue(stringWriter, parentClass);
String serialised = stringWriter.toString();
System.out.println(serialised);
System.out.println(xmlString.equals(serialised));
}
public static class ChildClass {
#JacksonXmlProperty(isAttribute = true)
private String name;
#JacksonXmlProperty(isAttribute = true)
private String value;
//getters & setters
}
public static class BrokenChild {
#JacksonXmlProperty(isAttribute = true)
private String name;
#JacksonXmlProperty(isAttribute = true)
private String reason;
//getters & setters
}
public static class ParentClass {
private final ChildClass childClass;
private final List<BrokenChild> brokenChildList;
#JsonCreator
public ParentClass(
#JsonProperty("childClass") ChildClass childClass,
#JsonProperty("brokenChildList") List<BrokenChild> brokenChildList
) {
this.childClass = childClass;
this.brokenChildList = brokenChildList;
}
#JacksonXmlProperty(localName = "childClass")
public ChildClass getChildClass() {
return childClass;
}
#JacksonXmlElementWrapper(localName = "brokenChildList")
#JacksonXmlProperty(localName = "brokenChild")
public List<BrokenChild> getBrokenChildList() {
return brokenChildList;
}
}
}
The above code gives output with Jackson version 2.8.10:
<ParentClass><childClass name="name1" value="val1"/><brokenChildList><brokenChild name="bc1" reason="bc-val1"/><brokenChild name="bc2" reason="bc-val2"/></brokenChildList></ParentClass>
true
With Jackson version 2.9.0 it gives:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Duplicate property 'brokenChildList' for [simple type, class org.test.Main$ParentClass]
at [Source: (StringReader); line: 1, column: 1]
I would like to find a solution (and any version after 2.9.0) that will give same output with the attached code.
My failed attempts include:
Replacing #JacksonXmlElementWrapper(localName = "brokenChildList") with #JacksonXmlElementWrapper will rename wrapper element as 'brokenChild' which is undesirable.
Removing #JacksonXmlElementWrapper(localName = "brokenChildList") will rename wrapper element as 'brokenChild' which is undesirable.
This problem is really tricky because Jackson collects metadata from different places: fields, getters, setters, constructor parameters. Also, you can use MixIn but in your case it does not appear.
#JacksonXmlElementWrapper annotation can be attached to FIELD and METHOD type elements and this forces you to declare it on getter. Because ParentClass is immutable and you want to build it with constructor we need to annotate constructor parameters as well. And this is where collision appears: you have a constructor parameter with #JsonProperty("brokenChildList") annotation and getter with #JacksonXmlElementWrapper(localName = "brokenChildList") which reuses the same name. If you would changed localName to #JacksonXmlElementWrapper(localName = "brokenChildListXYZ") (added XYZ) everything would be deserialised and serialised but output would be different then input.
To solve this problem, we can use com.fasterxml.jackson.databind.deser.BeanDeserializerModifier class which allows to filter out fields we do not want to use for deserialisation and which creates collision. Example usage:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.stream.Collectors;
public class XmlMapperApp {
public static void main(String... args) throws IOException {
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
#Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) {
if (beanDesc.getBeanClass() == ParentClass.class) {
return propDefs.stream().filter(p -> p.getConstructorParameter() != null).collect(Collectors.toList());
}
return super.updateProperties(config, beanDesc, propDefs);
}
});
XmlMapper xmlMapper = XmlMapper.xmlBuilder()
.addModule(module)
.build();
//yours code
}
}
To create this example I used version 2.10.0.
See also:
Jackson 2.10 features
Jackson Release 2.10
I am trying to utilize JAXB to convert xml elements gained from a youtube rss feed into objects. I seem to be following the structure of most examples I have seen, but still cannot get it to work as it always seem the list within feed is empty at all times. Does anyone know how to fix this?
Here are my classes for reference:
feed class:
import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.*;
#XmlRootElement(
name = "feed",
namespace = "http://www.w3.org/2005/Atom"
)
#XmlAccessorType (XmlAccessType.FIELD)
public class feed {
#XmlElement(name = "entry")
private List<entry> entries;
public List<entry> getEntry() {
return this.entries;
}
public void setEntry(List<entry> entries) {
this.entries = entries;
}
}
Entry class:
import javax.xml.bind.annotation.*;
import java.util.List;
#XmlRootElement(name = "entry")
#XmlAccessorType(XmlAccessType.FIELD)
public class entry {
private String title, name, id, published;
public void settitle(String title){this.title = title;}
public String gettitle(){return title;}
public void setname(String name){this.name = name;};
public String getname() {
return name;
}
public void setid(String id){this.id = id;}
public String getid() {
return id;
}
public void setpublished(String published){this.published = published;}
public String getpublished() {return published;}
public void PrintVideoInfo(){
System.out.println(gettitle());
System.out.println(getname());
System.out.println(getid());
System.out.println(getpublished());
System.out.println("-----------");
}
}
unmarshalling class:
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XMLtoObject {
public static void main(String[] args) {
try {
File file = new File("videos.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(feed.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
feed que= (feed) jaxbUnmarshaller.unmarshal(file);
for(entry ent:que.getEntry())
ent.PrintVideoInfo();
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
https://www.youtube.com/feeds/videos.xml?channel_id=UCBcRF18a7Qf58cCRy5xuWwQ this is the xml being used. It is saved as videos.xml and the file path is correct, just not fully included for privacy in the above snippet. Any help would be greatly appreciated.
Please, make your class names follow Pascal notation - it's way easier to read and understand, especially when there's a lot of code. Probably, you should work on your code style more.
Your feed class is annotated correctly, but in order for JAXB to unmarshal XML into entries entry class should also be annotated properly. Also note, that your field names do not match tag names in the xml file provided (<name> is not directly accessible within <feed>).
So, add annotations and classes to match xml file structure.
UPDATE 1
I realized I didn't quite understand xml documents, so I made a little investigation.
If you're completely familiar with xml namespaces, then you can skip this part to Serialization section. Otherwise, read on.
Namespace is a mechanism of dividing xml nodes into non-intersecting sets. Imagine having an <address> tag in your xml, where the tag is defined many times. It could either refer to a web address or a street address, for example, and thus have completely different meaning depending on context. To avoid confusion, you add a namespace prefix like that: <web:address> <street:address> to separate them and group other elements referring to the same namespace. You define them earlier in some root tag:
<root xmlns:web="Web">
<web:address> ... </web:address>
</root>
Also, there's a special namespace - default namespace - which is defined like this: <feed xmlns="Name">. Having a default namespace allows you to omit writing namespace prefix every time you define an xml element.
Let's clear this up with your xml example: it states three namespaces for <feed> element (yt, media, and the default namespace)
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns="http://www.w3.org/2005/Atom">
That means every element inside <feed> is kind of implicitly prefixed with the default namespace.
Intro to XML namespaces
Atom namespace explained
Serialization
I downloaded the xml file you provided and did some tests. It turned out, that JAXB just "didn't see" the entity tags, since they are hidden behind the default namspace, and we never said to JAXB there is a namespace at all, except for the <feed> element.
So, the solution is to annotate the elements you want to deserialize with a namespace, so that JAXB could understand that.
UPDATE 2
It looked as if the solution provided above was too messy: having to annotate every other element with a namespace is truly a violation of DRY principle. Fortunately, there is a solution to add a default namespace in just one line.
Create a file called package-info.java and add the following in it and replace package with yours:
#XmlSchema(
namespace = "http://www.w3.org/2005/Atom",
elementFormDefault = XmlNsForm.QUALIFIED
)
package package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
All it does is defines an xml schema for the document we want to parse. You can now remove all namespace = "..." lines and pretty up the code
If you're not familiar with xml schemas, check it out as it's a great way to keep control of xml documents structure.
The code after update 2:
Feed class
import javax.xml.bind.annotation.*;
import java.util.List;
#XmlRootElement(name = "feed")
#XmlAccessorType(XmlAccessType.FIELD)
public class Feed {
#XmlElement(name = "entry")
private List<Entry> entries;
public List<Entry> getEntries() {
return this.entries;
}
}
Entry class
import javax.xml.bind.annotation.*;
import java.util.Date;
#XmlRootElement(name = "entry")
#XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
#XmlElement(name = "title")
private String title;
#XmlElement(name = "id")
private String id;
#XmlElement(name = "published")
private Date datePublished;
#XmlElement(name = "author")
private Author author;
public String toString(){
return String.format("Id: %s, Title: %s, Author: %s, Published: %s",
id,
title,
author.toString(),
datePublished.toString());
}
}
Author class
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "author")
#XmlAccessorType(XmlAccessType.FIELD)
public class Author {
#XmlElement(name = "name")
private String name;
#XmlElement(name = "url")
private String url;
public String getName() {
return name;
}
public String getUrl() {
return url;
}
#Override
public String toString() {
return getName();
}
}
Main
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.File;
public class Main {
public static void main(String[] args) throws JAXBException {
Feed feed = (Feed) JAXBContext
.newInstance(Feed.class)
.createUnmarshaller()
.unmarshal(new File("youtube_feed.xml"));
for (Entry entry : feed.getEntries()) {
System.out.println(entry.toString());
}
}
}
Reading comprehension
JAXB and XML namespaces
Oracle JAXB annotations reference
I'm trying to convert a Java bean into an xml document and I'm having trouble with some of these more complex interfaces. Here is the setup:
protected Set<Object> field1;
protected Map<Integer, List<Object>> field2;
protected List<String> field3;
protected List<Object> field4;
protected List<Object> field5;
protected List<Object> field6;
protected List<String> field7;
protected List<Object> field8;
In each Object (which is itself a bean) I have the following at the top of each class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"field1",
"field2",
"field3",
"field4",
"field5",
"field6",
"field7",
"field8"
})
#XmlRootElement(name = "root")
I keep getting an exception for the Map of Integers and Lists when I marshal the whole bean. Is there something that I'm missing?
Suppose you have below three class
Customer class
package comparison;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private long id;
private String name;
private Address address;
private List<phonenumber> phoneNumbers;
public Customer() {
phoneNumbers = new ArrayList<PhoneNumber>();
}
}
Address class
package comparison;
public class Address {
private String city;
private String street;
}
and PhoneNumber class
package comparison;
public class PhoneNumber {
private String type;
private String number;
}
Now adding some dummy data
package comparison;
public class Data {
public static Customer CUSTOMER;
static {
CUSTOMER = new Customer();
CUSTOMER.setId(123);
CUSTOMER.setName("Jane Doe");
Address address = new Address();
address.setStreet("1 A Street");
address.setCity("Any Town");
CUSTOMER.setAddress(address);
PhoneNumber workPhoneNumber = new PhoneNumber();
workPhoneNumber.setType("work");
workPhoneNumber.setNumber("555-WORK");
CUSTOMER.getPhoneNumbers().add(workPhoneNumber);
PhoneNumber cellPhoneNumber = new PhoneNumber();
cellPhoneNumber.setType("cell");
cellPhoneNumber.setNumber("555-CELL");
CUSTOMER.getPhoneNumbers().add(cellPhoneNumber);
}
}
So now you apply marshalling to convert the object to xml
package comparison.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import comparison.Customer;
import static comparison.Data.CUSTOMER;
public class JAXBDemo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<Customer> jaxbElement = new JAXBElement<Customer>(new QName("customer"), Customer.class, CUSTOMER);
marshaller.marshal(jaxbElement, System.out);
}
}
A JAXBContext needs to be initialized on the binding metadata before
the marshal operation can occur.
Unlike XStream JAXB does not format
the XML by default, so we will enable this feature.
With no metadata
specified we need to supply JAXB with a root element name (and
namespace).
The code will produce result:
<customer>
<id>123</id>
<address>
<city>Any Town</city>
<street>1 A Street</street>
</address>
<name>Jane Doe</name>
<phoneNumbers>
<number>555-WORK</number>
<type>work</type>
</phoneNumbers>
<phoneNumbers>
<number>555-CELL</number>
<type>cell</type>
</phoneNumbers>
By default JAXB will access public fields and properties. You can configure JAXB to use field access with the following package level annotation.
#XmlAccessorType(XmlAccessType.FIELD)
package comparison;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
You can look at this blogpost for details.
I am trying to parse an xml to java objects, I've read and implemented the following tutorial:
http://www.vogella.com/articles/JAXB/article.html (works perfectly)
But when I create my own clases (similar to those in the tutorial)
I get: Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "clienteList"
Unless I use #XmlAccessorType(XmlAccessType.FIELD) on class Clientes but in the tutorial is not being used.
Any ideas ?
(It works fine with the #XmlAccessorType(XmlAccessType.FIELD) annotation but I want to know why is it being required with my classes while it is not for the classes in the tutorial)
Thank you in advance for any information.
Class Cliente
package mx.com.findep.crediseguros.dto.servicios.finsol;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "cliente")
public class Cliente {
private String numeroPersona;
#XmlElement(name = "numeroPersona")
public String getNumeroPersona() {
return numeroPersona;
}
public void setNumeroPersona(String numeroPersona) {
this.numeroPersona = numeroPersona;
}
}
Class Clientes
package mx.com.findep.crediseguros.dto.servicios.finsol;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "clientes")
//#XmlAccessorType(XmlAccessType.FIELD) //without this line it fails
public class Clientes {
// XmLElementWrapper generates a wrapper element around XML representation
#XmlElementWrapper(name = "clienteList")
// XmlElement sets the name of the entities
#XmlElement(name = "cliente")
private ArrayList<Cliente> clienteList;
public void setClienteList(ArrayList<Cliente> clienteList) {
this.clienteList = clienteList;
}
public ArrayList<Cliente> getClienteList() {
return clienteList;
}
}
Testing My Marshalling
package mx.com.findep.crediseguros.dto.servicios.finsol;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class TestClientesXml {
private static final String SOME_XML = "C:/bea/user_projects/domains/DominioDesarrollo/esquemas/calculoCostoSeguroPeticion.xml";
public static void main(String[] args) throws JAXBException, IOException {
ArrayList<Cliente> clienteList = new ArrayList<Cliente>();
Cliente cliente1 = new Cliente();
cliente1.setNumeroPersona("1");
clienteList.add(cliente1);
Cliente cliente2 = new Cliente();
cliente2.setNumeroPersona("2");
clienteList.add(cliente2);
Clientes clientes = new Clientes();
clientes.setClienteList(clienteList);
JAXBContext context = JAXBContext.newInstance(Clientes.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(clientes, System.out);
m.marshal(clientes, new File(SOME_XML));
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
Clientes clientes2 = (Clientes) um.unmarshal(new FileReader(SOME_XML));
ArrayList<Cliente> list = clientes2.getClienteList();
for (Cliente cliente : list) {
System.out.println("Cliente: " + cliente.getNumeroPersona());
}
}
}
By default JAXB treats public fields and properties as mapped. If you annotate a field it then considers the field and property as mapped causing the conflict. Without #XmlAccessorType(XmlAccessType.FIELD) you should annotate the get or set method.
For More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
So lets say we have:
#XmlRootElement(name = "book")
public class Book {
#XmlElement(name = "book_title")
private String title;
public getTitle(){..}
public setTitle(){...}
}
if we run the code we will have
Exception in thread "main"
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "title"
this problem is related to the following location:
at public java.lang.String com.example.jaxb.Book.getTitle()
at com.example.jaxb.Book
this problem is related to the following location:
at private java.lang.String com.example.jaxb.Book.title
at com.example.jaxb.Book
But if we add the annotation: XmlAccessorType
#XmlRootElement(name = "book")
#XmlAccessorType(XmlAccessType.FIELD)
public class Book {
the error will disappear.
When have class which want to marshall and have 10 fields, I prefer to annotate only fields, not one time the setter then the getter. So use #XmlAccessorType and annotate only the fields.
I have two classes ParentClass and ChildClass in JAVA using JAXB.
ChildClass extends ParentClass.
When I serialize an object of ChildClass, in the resulting XML, ParentClass properties appear first, I would like to have ChildClass properties first and then ParentClass properties.
Is this possible?
Thank you
The reason JAXB does this is to match inheritance in XML schema. However, you could do something like the following:
Mark the parent #XmlTransient
Set the propOrder on the child class
Parent
import javax.xml.bind.annotation.XmlTransient;
#XmlTransient
public abstract class Parent {
private String parentProp;
public String getParentProp() {
return parentProp;
}
public void setParentProp(String parentProp) {
this.parentProp = parentProp;
}
}
Child
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement
#XmlType(propOrder={"childProp", "parentProp"})
public class Child extends Parent {
private String childProp;
public String getChildProp() {
return childProp;
}
public void setChildProp(String childProp) {
this.childProp = childProp;
}
}
Demo
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Child.class);
Child child = new Child();
child.setParentProp("parent-value");
child.setChildProp("child-value");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(child, System.out);
}
}
Output
<child>
<childProp>child-value</childProp>
<parentProp>parent-value</parentProp>
</child>