create nested structure using JAXB notations - java

<?xml version='1.0'?>
<info>
<contract>
<symbol>IBM</symbol>
<sectype>STK</sectype>
<exchange>SMART</exchange>
<currency>USD</currency>
</contract>
<order>
<action>SELL</action>
<quantity>100</quantity>
<ordertype>LMT</ordertype>
<imtprice>imtprice</imtprice>
<transmit>false</transmit>
</order>
</info>
I want to use jaxb annotations with existing java classes to create above XML input but i don't know how create nested xml structure based on Java classes

Try this:
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(propOrder =
{"contract", "order"}) public class Info
{ #XmlElement(required =
true) private Contract
contract; #XmlElement(required = true) private Order order;
// Getters and setters }
Another class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(propOrder = {"symbol",
"sectype", "exchange",
"currency"}) public class
Contract { #XmlElement(required
= true) private String symbol; #XmlElement(required =
true) private String
sectype; #XmlElement(required =
true) private String
exchange; #XmlElement(required
= true) private String currency; //Getters and
setters}
Create an order class the same way.

Related

How can I haver property order in JAXB with nested list and objects

I would like to ask on how to marshal an object with property order from a nested object.
#XmlRootElement(name = "sample")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(propOrder = {"title", "code"})
public class SampleObject {
#XmlAttribute(name = "title")
private String title;
#XmlAttribute(name = "code")
private String code;
}
I have a wrapperList to set that object:
#XmlRootElement(name = "listWrapper")
#XmlAccessorType(XmlAccessType.FIELD)
public class WrapperObject {
#XmlAnyElement(lax=true)
private List<SampleObject> objectList;
}
And i want to set the list on this object. This object is the one who is being marshalled.
#XmlRootElement(name = "marshaller")
#XmlAccessorType(XmlAccessType.FIELD)
public class MarshallerObject {
#XmlElement(name = "wrapperList")
private WrapperObject objectList;
}
This is the output that i'm aiming for:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<marshaller>
<wrapperList>
<sample title="sampleTitle code"001"/>
</wrapperList>
</marshaller>
</soap:Envelope>
Thanks in advance!

Jackson XML ArrayList output having two wrapper elements

I am getting two wrapper elements in the XML output generated by Jackson.
I would like to have only one.
I have a Java bean
#Entity
#Table(name = "CITIES")
#JacksonXmlRootElement(localName = "City")
public class City implements Serializable {
private static final long serialVersionUID = 21L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JacksonXmlProperty(isAttribute = true)
private Long id;
#JacksonXmlProperty
private String name;
#JacksonXmlProperty
private int population;
// getters, setters etc
}
and a List wrapper class.
#JacksonXmlRootElement
public class Cities implements Serializable {
private static final long serialVersionUID = 22L;
#JacksonXmlProperty(localName = "City")
#JacksonXmlElementWrapper(localName = "MyCities")
private List<City> cities = new ArrayList<>();
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
I am getting this output, which has two wrapper elements. I would like
to remove one of them.
<Cities>
<MyCities>
<City id="1">
<name>Bratislava</name>
<population>432000</population>
</City>
<City id="2">
<name>Budapest</name>
<population>1759000</population>
</City>
<City id="3">
<name>Prague</name>
<population>1280000</population>
</City>
<MyCities>
</Cities>
One of them comes from the ArrayList, one from the class. How to get rid of one of the wrapper elements?
What I want to have is this:
<Cities>
<City id="1">
<name>Bratislava</name>
<population>432000</population>
</City>
<City id="2">
<name>Budapest</name>
<population>1759000</population>
</City>
<City id="3">
<name>Prague</name>
<population>1280000</population>
</City>
</Cities>
"Cities" is the root element, not a wrapper. Wouldn't removing the actual wrapper element "MyCities" work?
Adding #JacksonXmlElementWrapper(useWrapping = false) could also help.
Replacing #JacksonXmlElementWrapper(localName = "MyCities") with #JacksonXmlElementWrapper(useWrapping = false) in Cities should remove the additional wrapper element.
From the documentation:
#JacksonXmlElementWrapper
Allows specifying XML element to use for
wrapping List and Map properties; or disabling use (with 'useWrapping'
set to false).
The fix implemented in Cities:
#JacksonXmlRootElement
public class Cities implements Serializable {
private static final long serialVersionUID = 22L;
#JacksonXmlProperty(localName = "City")
#JacksonXmlElementWrapper(useWrapping = false)
private List<City> cities = new ArrayList<>();
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
You could also disable the wrapping functionality directly in the mapper with mapper.setDefaultUseWrapper(false);.
In this case you should simply remove #JacksonXmlElementWrapper(localName = "MyCities") from cities.

JAXB: how to unmarshal a List of objects of different types inside a wrapper?

I'm stucked at parsing the following xml with JAXB:
<?xml version="1.0" encoding="utf-8"?>
<dashboardreport name="exampleDashboard" version="6.5.6.1013" reportdate="2016-12-16T11:05:19.329+01:00" description="">
<data>
<incidentchartdashlet name="Incident Chart" description="" />
<chartdashlet name="WebRequestTime" showabsolutevalues="false" />
<chartdashlet name="WebServiceTime" showabsolutevalues="false" />
</data>
</dashboardreport>
I used the following java classes to unmarshal the xml:
Dashboardreport.java
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "dashboardreport")
public class Dashboardreport {
#XmlElementWrapper(name = "data")
#XmlElement(name = "chartdashlet")
protected List<Chartdashlet> chartdashlets;
#XmlElementWrapper(name = "data")
#XmlElement(name = "incidentchartdashlet")
protected List<Incidentchartdashlet> incidentchartdashlets;
#XmlAttribute(name = "name")
protected String name;
}
I just want to unmarshal the xml without using a wrapper class around incidentchartdashlets and chartdashlet, cause both types differ a lot.
I only can use the XmlElementWrapper annotation once, so that only chartdashlets get filled and incidentchartdashlets is null.
Is there any solution with JAXB without using a seperate wrapper class?
I assume your dashlet classes are defined like
class Chartdashlet extends Dashlet and class Incidentchartdashlet extends Dashlet.
Then the preferred JAXB way to handle your mixed list of dashlets would be
by using the #XmlElements
annotation:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "dashboardreport")
public class Dashboardreport {
#XmlElementWrapper(name = "data")
#XmlElements({
#XmlElement(name = "chartdashlet", type = Chartdashlet.class),
#XmlElement(name = "incidentchartdashlet", type = Incidentchartdashlet.class)
})
protected List<Dashlet> dashlets;
#XmlAttribute(name = "name")
protected String name;
}

Camel don't want to print POJO in java DSL route

I'm trying to use Apache Camel to parse XML to POJO and have problem with printing out the actual POJO. Instead I get regular XML as if no convertation not happening. When I pass for example Customer insted of Customers its working okay. Also printing Customers class to sout in bean warks perfectly.
MyRoute
#Autowired
private MyBean mb;
#Override
public void configure() throws Exception {
from("file:{{customer.path}}?noop=true")
.bean(mb)
.to("stream:out");
}
MyBean
#Handler
public Customers whatIsInBody(Customers body) {
return body;
}
POJO classes:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"customer"
})
#XmlRootElement(name = "customers")
public #Data
class Customers {
#XmlElement(required = true, nillable = true)
protected List<Customer> customer;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "customer", propOrder = {
"id",
"name",
"adress",
"countryCode",
"products"
})
public #Data
class Customer {
protected long id;
#XmlElement(required = true)
protected String name;
#XmlElement(required = true)
protected String adress;
#XmlElement(required = true)
protected String countryCode;
#XmlElement(required = true, nillable = true)
protected List<Product> products;
}
Output example:
<customers>
<customer>
<id>12345</id>
<name>str1234</name>
<adress>str1234</adress>
<countryCode>str1234</countryCode>
<products>
<id>12345</id>
<name>str1234</name>
</products>
</customer>
Desired output:
Customers(customer=[Customer(id=12345, name=str1234, adress=str1234, countryCode=str1234, products=[Product(id=12345, name=str1234)]),
When you have camel-jaxb on the classpath, then a POJO class with JAXB annotations is type converted to XML when you convert it to a String type, which is what the stream:out would do.
You can enable the tracer to see what the message contains during routing
http://camel.apache.org/tracer
So if you really want to stream:out the toString of the message body, you would need to transform it manually first, by calling its toString method
.transform(simple("${body.toString()}"))

How to generate xml tag as full class name using jaxb?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution>
<insert out-identifier="employee" return-object="true" entry-point="DEFAULT">
<fact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<name>Rajashekhar</name>
<age>21</age>
</fact>
</insert>
<fire-all-rules></fire-all-rules>
</batch-execution>
Now I am getting this output as above but I want out put like below
<batch-execution>
<insert out-identifier="employee" return-object="true"
entry-point="DEFAULT">
<com.practise.Employee>
<name>Rajashekhar</name>
<age>21</age>
</com.practise.Employee>
</insert>
<fire-all-rules />
</batch-execution>
My Jaxb classes are
Request.java
#XmlRootElement(name = "batch-execution")
#XmlAccessorType(XmlAccessType.FIELD)
public class Request implements Serializable {
#XmlElement(name = "insert")
private List<Insert> insert;
#XmlElement(name = "fire-all-rules",nillable=true)
private String fireAllRules = "";
.
..
setters and getter
Insert.java
#XmlAccessorType(XmlAccessType.FIELD)
public class Insert {
#XmlAttribute(name = "out-identifier", required = true)
private String outIdentifier;
#XmlAttribute(name = "return-object")
private boolean returnObject;
#XmlAttribute(name = "entry-point")
private String entryPoint;
private Object fact;
.
.
setters and gettes
com.practise.Employee.java
#XmlRootElement(name="kewill.com.kewill.practoise.Employee")
#XmlAccessorType(XmlAccessType.FIELD)
public class Employee implements java.io.Serializable
{
static final long serialVersionUID = 1L;
#org.kie.api.definition.type.Label("Name")
private java.lang.String name;
#org.kie.api.definition.type.Label("Id")
private java.lang.Integer id;
#org.kie.api.definition.type.Label("Age")
private int age;
#org.kie.api.definition.type.Label(value = "valid")
private java.lang.Boolean valid;
.
. setters and getters
I think it is possible through xtream api but I want to use JAXB please provide me the solution in jaxb.
In Insert.java
added Annotation for
private Object fact;
as
#XmlAnyElement(lax = true)
private Object fact;
Now it is giving expected output.

Categories

Resources