Here is my code
class
#Data
#XmlRootElement(name = "ASX", namespace = "urn:synt:aps:zion")
#XmlAccessorType(XmlAccessType.FIELD)
public class Cpix {
#XmlElement(name = "ZionicList")
private ZionicList zionicList;
#XmlElement(name = "Abrah")
private AbrahList abrahList;
}
rsp
<ns2:ASX xmlns="urn:djwfw:fwd2" xmlns:ns2="urn:synt:aps:zion">
<ns2:ZionicList>
....
</ns2:ZionicList>
<ns2:Abrah>
....
</ns2:Abrah>
</ns2:ASX>
Converter
public static <T> T convertXmlToObject(String response, Class<T> clazz) {
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(response));
} catch (JAXBException e) {
fail(e.getMessage());
}
return null;
}
In the end Im getting null for ZionicList and AbrahList
It's ok when Im explicitly adding namespace to each xmlElement attribute
#Data
#XmlRootElement(name = "ASX", namespace = "urn:synt:aps:zion")
#XmlAccessorType(XmlAccessType.FIELD)
public class Cpix {
#XmlElement(name = "ZionicList", namespace = "urn:synt:aps:zion")
private ZionicList zionicList;
#XmlElement(name = "Abrah", namespace = "urn:synt:aps:zion")
private AbrahList abrahList;
}
Related
I need to create a XML which matches this structure:
<OfferPackage Name="aaa">
<OfferPackage.Offers>
<OfferCollection Capacity="1">
<Offer Price="12.34"/>
<Offer Price="12.34"/>
</OfferCollection>
</OfferPackage.Offers>
</OfferPackage>
My problem is that I do not know how to achieve this here: <OfferPackage.Offers>. How I can get this "dot notation" with just annotations? All I got out looks like this:
<OfferPackage Name="aaa">
<OfferCollection Capacity="1">
<Offer Price="12.34"/>
<Offer Price="12.34"/>
</OfferCollection>
</OfferPackage>
This is my current code:
#XmlRootElement(name = "OfferPackage")
#XmlAccessorType(XmlAccessType.FIELD)
public class OfferPackage {
#XmlAttribute(name = "Name")
private String name = null;
#XmlElement( name = "OfferCollection")
private List<OfferCollection> offers = null;
#SneakyThrows
public String toString() {
var sw = new StringWriter();
var marshaller = JAXBContext
.newInstance(OfferPackage.class)
.createMarshaller();
marshaller.setProperty(JAXB_FRAGMENT, true);
marshaller.marshal(this, sw);
return sw.toString();
}
}
#XmlRootElement(name = "Offer")
#XmlAccessorType(XmlAccessType.FIELD)
public class OfferCollection {
#XmlAttribute(name = "Capacity")
private Integer capacity = null;
#XmlElement(name = "Offer")
private List<Offer> offer = null;
}
You can achieve the desired behavior using the #XmlElementWrapper annotation:
Generates a wrapper element around XML representation. This is primarily intended to be used to produce a wrapper XML element around collections.
In your case, it will look similar to this:
#XmlRootElement(name = "OfferPackage")
#XmlAccessorType(XmlAccessType.FIELD)
public class OfferPackage {
#XmlAttribute(name = "Name")
private String name = null;
#XmlElementWrapper(name="OfferPackage.Offers")
#XmlElement( name = "OfferCollection")
private List<OfferCollection> offers = null;
#SneakyThrows
public String toString() {
var sw = new StringWriter();
var marshaller = JAXBContext
.newInstance(OfferPackage.class)
.createMarshaller();
marshaller.setProperty(JAXB_FRAGMENT, true);
marshaller.marshal(this, sw);
return sw.toString();
}
}
Other possibility will be to define an intermediate class, OfferPackageOffers to wrap the OfferCollection List::
#XmlRootElement(name = "OfferPackage.Offers")
#XmlAccessorType(XmlAccessType.FIELD)
public class OfferPackageOffers {
#XmlElement( name = "OfferCollection")
private List<OfferCollection> offers = null;
//...
}
And use the new class in OfferPackage instead of the mentioned OfferCollection List:
#XmlRootElement(name = "OfferPackage")
#XmlAccessorType(XmlAccessType.FIELD)
public class OfferPackage {
#XmlAttribute(name = "Name")
private String name = null;
#XmlElement( name = "OfferPackage.Offers")
private OfferPackageOffers offers = null;
//...
}
I want to parse out different types based on xml, when header=1 then User, header=2 then Order etc. for example:
<entity>
<header>1</header>
<body>
<userId>1</userId>
<userName>jonh</userName>
...
<body>
</entity>
<entity>
<header>2</header>
<body>
<orderId>1</orderId>
<orderNo>20200101</orderNo>
...
<body>
</entity>
How to implement this function?
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object object = unmarshaller.unmarshal(xml);
I would try this: provide the type when unmarshalling. So maybe do something like this: create a transient facade object:
public class XmlEntityFacade {
private int header;
private Object body;
//getters and setters...
}
And then cast this type while unmarshalling:
...
XmlEntityFacade facade = (XmlEntityFacade) unmarshaller.unmarshal(xml);
Then you can access the value of the by calling .getHeader() and body with .getBody() (getters that you have provided XmlEntityFacade class). And then depending on the value cast the required type to the Object.
public class TwiceUnmarshalTest {
#Data
#ToString
public static abstract class HeaderResponse {
private String header;
}
#XmlRootElement(name = "entity")
#XmlAccessorType(XmlAccessType.FIELD)
public static class XmlHeaderResponse extends HeaderResponse {
}
private final String xml = "<entity>" +
" <header>2</header>" +
" <body>" +
" <orderId>1</orderId>" +
" <orderNo>2020</orderNo>" +
" </body>" +
"</entity>";
#SuppressWarnings("unchecked")
public static <T> T unmarshal(Reader reader, Class<T> typeClass) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(reader);
}
#Test
public void headerResponse() throws Exception {
HeaderResponse response = unmarshal(new StringReader(xml), XmlHeaderResponse.class);
System.out.println(response);
}
#ToString(callSuper = true)
public static abstract class Response<T> extends HeaderResponse {
#XmlAnyElement(lax = true)
public T body;
}
#Data
#XmlRootElement(name = "body")
public static class Order {
private String orderId;
private String orderNo;
}
#XmlRootElement(name = "entity")
#XmlSeeAlso({Order.class})
public static class OrderResponse extends Response<Order> {
}
#Test
public void response() throws Exception {
XmlHeaderResponse response = unmarshal(new StringReader(xml), XmlHeaderResponse.class);
System.out.println(response);
//TwiceUnmarshalTest.HeaderResponse(header=2)
if (response.getHeader().equals("2")) {
OrderResponse orderResponse = unmarshal(new StringReader(xml), OrderResponse.class);
System.out.println(orderResponse);
//TwiceUnmarshalTest.Response(super=TwiceUnmarshalTest.HeaderResponse(header=2), body=TwiceUnmarshalTest.Order(orderId=1, orderNo=2020))
}
}
}
unmarshal twice, just get the header first, then get the entity. Not very good but can be used.
Is there any way we can un-marshall for a class without #XmlRootElement annotation? Or are we obligated to enter the annotation?
for example:
public class Customer {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
#XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
#XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
#XmlAttribute
public void setId(int id) {
this.id = id;
}
}
and let the unmarshalling code for properly annotated class be like:
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
leaving out the details.
Following code is used to marshall and unmarshall withot #XmlRootElement
public static void main(String[] args) {
try {
StringWriter stringWriter = new StringWriter();
Customer c = new Customer();
c.setAge(1);
c.setName("name");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(new JAXBElement<Customer>( new QName("", "Customer"), Customer.class, null, c), stringWriter);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream is = new ByteArrayInputStream(stringWriter.toString().getBytes());
JAXBElement<Customer> customer = (JAXBElement<Customer>) jaxbUnmarshaller.unmarshal(new StreamSource(is),Customer.class);
c = customer.getValue();
} catch (JAXBException e) {
e.printStackTrace();
}
}
Above code works only if you adding #XmlAccessorType(XmlAccessType.PROPERTY) on Customer class, or make private all attributes.
If you cannot add XmlRootElement to existing bean you can also create a holder class and mark it with annotation as XmlRootElement. Example below:-
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class CustomerHolder
{
private Customer cusotmer;
public Customer getCusotmer() {
return cusotmer;
}
public void setCusotmer(Customer cusotmer) {
this.cusotmer = cusotmer;
}
}
It is indisputable that the question is very old for an answer, however still hoping someone like me is looking for a simple generic code snippet for the above requirement, hence sharing the working solution for the marshal and unmarshal need.
Marshalling XML object to String
public static <T> Optional<String> objectToXMLString(T value,Class<T> clazz,String rootElement) {
StringWriter sw = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
JAXBElement<T> jaxbElement = new JAXBElement<>(new QName(rootElement), clazz,value);
jaxbMarshaller.marshal(jaxbElement, sw);
return Optional.of(sw.toString());
} catch (JAXBException e) {
LOGGER.error("XML to String Conversion exception:{}", e.getMessage(), e);
}
return Optional.empty();
}
UnMarshalling String to XML object
public static <T> Optional<T> xmlStringToObject(String value, Class<T> clazz) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InputStream is = new ByteArrayInputStream(value.getBytes());
JAXBElement<T> object = unmarshaller.unmarshal(new StreamSource(is),clazz);
return Optional.of(object.getValue());
} catch (JAXBException e) {
LOGGER.error("String to XML Conversion exception : {}", e.getMessage(), e);
}
return Optional.empty();
}
I'm getting null value when unmarshelling the xml file to java class. But the xml file as values corresponding to the its attributes. Is there any mistake in my pojo class or unmarshelling?
Please help
This is my pojo
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "")
#XmlRootElement(name = "CardUpdateResponse",namespace="http://www.samople.com/Prepaid")
public class FVCardUpdateResponse {
#XmlElement(name = "AccountNumber")
private String AccountNumber;
#XmlElement(name = "ResCode")
private String ResCode;
#XmlElement(name = "ResErrorCode")
private String ResErrorCode;
#XmlElement(name = "ResErrorMsg")
private String ResErrorMsg;
//Setters and Getters
}
This is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<CardUpdateResponse xmlns="http://www.samople.com/Prepaid">
<CARDUPDATE_RET>
<ResErrorMsg>ID Issue Date must be equal or less than present date</ResErrorMsg>
<ResErrorCode>ErrIsud01</ResErrorCode>
<ResCode>0</ResCode>
<ACCOUNTNUMBER>2000000003918246</ACCOUNTNUMBER>
</CARDUPDATE_RET>
</CardUpdateResponse>
this is code for unmarshelling
public class XmlUnmarshelling {
public void unmarshell()
{
try
{
System.out.println("xml unmarshelling class");
File file = new File("D:/var/lib/tomcat7/webapps/tmpFiles/1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(FVCardUpdateResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
FVCardUpdateResponse CARDUPDATE_ret = (FVCardUpdateResponse) jaxbUnmarshaller.unmarshal(file);
System.out.println("xml unmarshelled = "+CARDUPDATE_ret.getResErrorMsg());//Getting null value as response.
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Your POJO doesn't have the same structure as your XML. CardUpdateResponse doesn't directly contain the properties in your POJO, it contains CARDUPDATE_RET element which contains the properties.
You could modify your POJO like this to match the XML:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "CardUpdateResponse", namespace="http://www.samople.com/Prepaid")
public static class CardUpdateResponseWrapper {
#XmlElement(name="CARDUPDATE_RET")
private FVCardUpdateResponse response;
// Getter and setter for response
public static class FVCardUpdateResponse {
#XmlElement(name = "AccountNumber")
private String AccountNumber;
#XmlElement(name = "ResCode")
private String ResCode;
#XmlElement(name = "ResErrorCode")
private String ResErrorCode;
#XmlElement(name = "ResErrorMsg")
private String ResErrorMsg;
// Getters and setters
}
}
Now the CardUpdateResponseWrapper class will represent your root XML element and it will have instance of FVCardUpdateResponse which will represent the CARDUPDATE_RET XML element.
Do unmarshall it, just call:
File file = new File("D:/var/lib/tomcat7/webapps/tmpFiles/1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(CardUpdateResponseWrapper.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CardUpdateResponseWrapper wrapper = (CardUpdateResponseWrapper) jaxbUnmarshaller.unmarshal(file);
System.out.println(wrapper.getResponse().getResErrorMsg());
I think that the issue is a combination of two problems, one what Bohuslav is saying, the other you need to repeat your namespace on every XmlElement annotation, e.g.
#XmlElement(name = "ResCode", namespace="http://www.samople.com/Prepaid")
and one particular issue, you need to match the cases as well so the name for AccountNumber should be capitalized ACCOUNTNUMBER
In the root.class from my xi-schema, the element item and ohter objects are part of an itemList:
#XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false)
//...
protected List<Object> itemList;
I've in the ObjectFactory.class from the main-schema some items as JAXBElements like this:
#XmlElementDecl(namespace = "de-schema", name = "detailedInformation", substitutionHeadNamespace = "xi", substitutionHeadName = "item")
public JAXBElement<numItemType> createDetailedInformation(numItemType num) {
return new JAXBElement<numItemType>(_detailedInformation_QNAME, numItemType.class, null, num);
}
So the numItemType has some attributes and value(num) for the JAXBElement.
NumItemType.class:
#XmlJavaTypeAdapter(numItemTypeAdapter.class)
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "numItemType", namespace = "xi", propOrder = {
"num"
})
public class NumItemType {
#XmlValue
protected BigDecimal num;
#XmlAttribute(name = "precision")
protected String precision;
#XmlAttribute(name = "decimals")
protected String decimals;
//... more Attributes
}
But when JAXB unmarshal the XML document, it will has only elements, for example:
<detailedInformation>
<element1>1234</element1>
<element2>5678</element2>
<element3>bla</element3>
</detailedInformation>
When I marshal it, it should become (like the JAXB java code):
<detailedInformation element2="5678" element3="bla">1234</detailedInformation>
Therefore, I have written an numItemTypeAdapter.class with
NumItemTypeAdapter extends XmlAdapter
AdaptedNum.class:
public class AdaptedNum {
#XmlElement
private double element1;
#XmlElement
private String element2;
#XmlElement
private String element3;
/** Some getter/setter methods */
}
I thought, that would be help me http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html, but it is all a bit tricky :-/
It's a bit tricky to sort out exactly where your problem may be occurring. I'm assuming your original model was generated from an XML Schema, this should work as is without any modifications. I've attempted below to provide a scaled down version of your example which may help.
Root
package forum11343610;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Root {
#XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false)
protected List<Object> itemList = new ArrayList<Object>();
public List<Object> getItemList() {
return itemList;
}
public void setItemList(List<Object> itemList) {
this.itemList = itemList;
}
}
NumItemType
package forum11343610;
import java.math.BigDecimal;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "numItemType", namespace = "xi", propOrder = {
"num"
})
public class NumItemType {
#XmlValue
protected BigDecimal num;
#XmlAttribute(name = "precision")
protected String precision;
#XmlAttribute(name = "decimals")
protected String decimals;
//... more Attributes
}
ObjectFactory
package forum11343610;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
#XmlRegistry
public class ObjectFactory {
private static final QName _detailedInformation_QNAME = new QName("de-schema", "detailedInformation");
#XmlElementDecl(namespace = "xi", name = "item")
public JAXBElement<NumItemType> createItem(NumItemType num) {
return new JAXBElement<NumItemType>(_detailedInformation_QNAME, NumItemType.class, null, num);
}
#XmlElementDecl(namespace = "de-schema", name = "detailedInformation", substitutionHeadNamespace = "xi", substitutionHeadName = "item")
public JAXBElement<NumItemType> createDetailedInformation(NumItemType num) {
return new JAXBElement<NumItemType>(_detailedInformation_QNAME, NumItemType.class, null, num);
}
}
Demo
package forum11343610;
import java.math.BigDecimal;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class, ObjectFactory.class);
ObjectFactory objectFactory = new ObjectFactory();
Root root = new Root();
NumItemType numItemType = new NumItemType();
numItemType.num = BigDecimal.TEN;
numItemType.decimals = "1";
numItemType.precision = "2";
root.getItemList().add(objectFactory.createDetailedInformation(numItemType));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:ns2="de-schema" xmlns:ns3="xi">
<ns2:detailedInformation precision="2" decimals="1">10</ns2:detailedInformation>
</root>
Thank your for your comment.
That's the point:
Demo
NumItemType numItemType = new NumItemType();
numItemType.num = BigDecimal.TEN;
numItemType.decimals = "1";
numItemType.precision = "2";
root.getItemList().add(objectFactory.createDetailedInformation(numItemType));
It should unmarshal and map the XML automatically.
XML Input
<detailedInformation>
<element1>1234</element1>
<element2>5678</element2>
<element3>bla</element3>
</detailedInformation>
With the Code:
Demo
JAXBContext jc = JAXBContext.newInstance(Root.class, ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
File xml = new File("D:/", "test.xml");
Root root = (Root) u.unmarshal(xml);
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:ns2="de-schema" xmlns:ns3="xi">
<ns2:detailedInformation precision="2" decimals="1">10</ns2:detailedInformation>
</root>
I could parse the XML Document with DOM to a tree and marhalling with JAXB...
Thank you!
In other words:
I want to set new elements to the NumItemType.class for the unmarshalling without change the schema java code.
NumItemType.class
package forum11343610;
import java.math.BigDecimal;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "numItemType", namespace = "xi", propOrder = {
"num"
})
#XmlJavaTypeAdapter(NumItemTypeAdapter.class)
public class NumItemType {
#XmlValue
protected BigDecimal num;
#XmlAttribute(name = "precision")
protected String precision;
#XmlAttribute(name = "decimals")
protected String decimals;
//... more Attributes
}
NumItemTypeAdapter.class
public class NumItemTypeAdapter extends XmlAdapter<AdaptedNum, NumItemType> {
#Override
public NumItemType unmarshal(AdaptedNum an) throws Exception {
NumItemType nit = new NumItemType();
nit.setNum(an.getNum);
nit.setPrecision(an.getPrecision);
nit.setDecimals(an.getDecimals)
return nit;
}
}
AdaptedNum.class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "adaptedNum", namespace = "", propOrder = {
"element1",
"element2",
"element3"
})
public class AdaptedNum {
#XmlElement(name ="element1")
protected BigDecimal num;
#XmlElement(name ="element2")
private String decimals;
#XmlElement(name ="element3")
private String precison;
// set/get method
}