Specifying root and child nodes with JAXB - java

Staying within JAXB how would I refactor MyNote so that it conforms to:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Which is well formed but not valid, to my understanding. Current output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyNotes>
<Note>
<note>XY3Z1RGEO9W79LALCS</note>
<to>LJAY9RNMUGGENGNND9</to>
<from>GOVSHVZ3GJWC864L7X</from>
<heading>EX6LGVE5LGY4A6B9SK</heading>
<body>L95WYQNMEU1MFDRBG4</body>
</Note>
</MyNotes>
which is too flat, rather than nested as the example.
I believe this makes note the root element, with other elements being children nodes to note if I'm using correct terminology.
The MyNote class:
package net.bounceme.dur.jaxb.hello.world;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = {"note", "to", "from", "heading", "body"})
#XmlRootElement(name = "note")
public class MyNote {
private String note;
private String to;
private String from;
private String heading;
private String body;
public String getNote() {
return note;
}
#XmlElement(name = "note")
public void setNote(String note) {
this.note = note;
}
public String getTo() {
return to;
}
#XmlElement(name = "to")
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
#XmlElement(name = "from")
public void setFrom(String from) {
this.from = from;
}
public String getHeading() {
return heading;
}
#XmlElement(name = "heading")
public void setHeading(String heading) {
this.heading = heading;
}
public String getBody() {
return body;
}
#XmlElement(name = "body")
public void setBody(String body) {
this.body = body;
}
#Override
public String toString() {
return note + to + from + heading + body;
}
}
The MyNotes class:
package net.bounceme.dur.jaxb.hello.world;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "MyNotes")
public class MyNotes {
private static final Logger LOG = Logger.getLogger(MyNotes.class.getName());
private List<MyNote> myNotes = new ArrayList<>();
public MyNotes() {
}
public List<MyNote> getMyNotes() {
LOG.info(myNotes.toString());
return myNotes;
}
#XmlElement(name = "Note")
public void setMyNotes(List<MyNote> myNotes) {
LOG.info(myNotes.toString());
this.myNotes = myNotes;
}
public void add(MyNote myNote) {
LOG.info(myNote.toString());
myNotes.add(myNote);
}
#Override
public String toString() {
StringBuffer str = new StringBuffer();
for (MyNote note : this.myNotes) {
str.append(note.toString());
}
return str.toString();
}
}
exercising the MyNote and MyNotes classes:
public MyNotes unmarshallMyNotesFromFile(URI uri) throws Exception {
File file = new File(uri);
JAXBContext jaxbContext = JAXBContext.newInstance(MyNotes.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MyNotes myNotes = (MyNotes) jaxbUnmarshaller.unmarshal(file);
return myNotes;
}
public void marshallMyNotesAndWriteToFile(MyNotes notes, URI uri) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(MyNotes.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(notes, new File(uri));
jaxbMarshaller.marshal(notes, System.out);
}
I'm looking to grab this xml through the web; first need to match the structure to the example.

You are very close. You need to change how you name your xmlElement for myNotes in MyNotes class. Also MyNote should not have a note field itself (according to your desired xml). Your edited classes would look like this (I also removed the logging statements for my convenience):
#XmlType(propOrder = { "to", "from", "heading", "body"})
#XmlRootElement(name = "note")
public class MyNote {
private String to;
private String from;
private String heading;
private String body;
public String getTo() {
return to;
}
#XmlElement(name = "to")
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
#XmlElement(name = "from")
public void setFrom(String from) {
this.from = from;
}
public String getHeading() {
return heading;
}
#XmlElement(name = "heading")
public void setHeading(String heading) {
this.heading = heading;
}
public String getBody() {
return body;
}
#XmlElement(name = "body")
public void setBody(String body) {
this.body = body;
}
#Override
public String toString() {
return to + from + heading + body;
}
}
and MyNotes:
#XmlRootElement(name = "MyNotes")
public class MyNotes {
private List<MyNote> myNotes = new ArrayList<>();
public MyNotes() {
}
public List<MyNote> getMyNotes() {
return myNotes;
}
#XmlElement(name = "note")
public void setMyNotes(List<MyNote> myNotes) {
this.myNotes = myNotes;
}
public void add(MyNote myNote) {
myNotes.add(myNote);
}
#Override
public String toString() {
StringBuffer str = new StringBuffer();
for (MyNote note : this.myNotes) {
str.append(note.toString());
}
return str.toString();
}
}

Related

How to deserialize object in java SimpleXML

i have to send request by soap. and i use Retrofit2.
i cant convert and deserialize the response. in type of xml like "xsi" instance "xmlns" its my problem.
this is my response
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:requestCPResponse xmlns:ns2="http://incomingwebchannel.hrz.jpos.org/">
<return xsi:type="ns2:jiringBillInqOut" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<refNo>091015900983</refNo>
<responseCode>03</responseCode>
</return>
</ns2:requestCPResponse>
</S:Body>
</S:Envelope>
and this is my retrofit interface
#POST("/nafis-web-3/services/mobile")
Call<UsStatesResponseEnvelope> requestCP(#Body UsStatesRequestEnvelope body);
this is my request code
new RequestCallback<>(activity, true, ApiClient.createApi(activity).requestCP(envelope), new RequestListener<UsStatesResponseEnvelope>() {
#Override
public void onResponse(#NonNull final Response<UsStatesResponseEnvelope> response) {
try {
com.teskaco.transactionsample.utils.Log.e(response.body().getBody().getUsStatesCPResponse().getStatesResponseReturn().getResponseCode());
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
}
#Override
public void onFailure(#NonNull int code, #NonNull JsonObject jsonObject) {
}
});
and this is my model. UsStatesResponseBody.clss
#Root(name = "S:Envelope")
#NamespaceList({
#Namespace( prefix = "S", reference = "http://schemas.xmlsoap.org/soap/envelope/")})
public class UsStatesResponseEnvelope {
#Element(name = "S:Body", required = false)
private UsStatesResponseBody body;
public UsStatesResponseBody getBody() {
return body;
}
public void setBody(UsStatesResponseBody body) {
this.body = body;
}
}
class UsStatesResponseBody.clss
#Root(name = "S:Body", strict = false)
public class UsStatesResponseBody {
#Element(name = "ns2:requestCPResponse",required = false)
private UsStatesCPResponse cpResponse;
public UsStatesCPResponse getUsStatesCPResponse() {
return cpResponse;
}
public void UsUsStatesCPResponse(UsStatesCPResponse responseReturn) {
this.cpResponse = responseReturn;
}
}
UsStatesCPResponse.clss
#Root(name = "ns2:requestCPResponse", strict = false)
#NamespaceList({
#Namespace( prefix = "ns2", reference = "http://incomingwebchannel.hrz.jpos.org/")})
public class UsStatesCPResponse {
#Element(name = "return",required = false)
private UsStatesResponseReturn responseReturn;
public UsStatesResponseReturn getStatesResponseReturn() {
return responseReturn;
}
public void UsStatesResponseReturn(UsStatesResponseReturn responseReturn) {
this.responseReturn = responseReturn;
}
}
UsStatesResponseReturn.clss
#Root(name = "return", strict = false)
#NamespaceList({
#Namespace( prefix = "xsi:type", reference = "ns2:jiringBillInqOut"),
#Namespace( prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance")
})
public class UsStatesResponseReturn {
#Element(name = "refNo",required = false)
private String refNo;
#Element(name = "responseCode",required = false)
private String responseCode;
public String getRefNo() {
return refNo;
}
public void setRefNo(String refNo) {
this.refNo = refNo;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
}
i solve with tray and error.
Parent class
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
#Root(name = "S:Envelope" ,strict = false)
#Namespace(prefix = "S", reference = "http://schemas.xmlsoap.org/soap/envelope/")
public class BuyResponseEnvelope {
#Element(name = "Body")
private BuyResponseBody body;
public BuyResponseBody getBody() {
return body;
}
public void setBody(BuyResponseBody body) {
this.body = body;
}
}
Body class
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
#Root(name = "S:Body" ,strict = false)
public class BuyResponseBody {
#Element(name = "requestCPResponse")
private BuyCPResponse cpResponse;
public BuyCPResponse getCpResponse() {
return cpResponse;
}
public void setCpResponse(BuyCPResponse cpResponse) {
this.cpResponse = cpResponse;
}
}
request cp class
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
#Root(name = "ns2:requestCPResponse",strict = false)
#Namespace( prefix = "ns2", reference = "http://incomingwebchannel.hrz.jpos.org/")
public class BuyCPResponse {
#Element(name = "return")
private BuyResponseReturn responseReturn;
public BuyResponseReturn getResponseReturn() {
return responseReturn;
}
public void setResponseReturn(BuyResponseReturn responseReturn) {
this.responseReturn = responseReturn;
}
}
and return class
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Root;
#Root(name = "return",strict = false)
#Namespace( prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance")
public class BuyResponseReturn {
#Element(name = "refNo")
private String refNo;
#Element(name = "responseCode")
private String responseCode;
public String getRefNo() {
return refNo;
}
public void setRefNo(String refNo) {
this.refNo = refNo;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
}

How do I reference a JAXB POJO in a mule flow

I have successfully unmarshalled an XML document into a JAXB object but now, I would like to reference the object in the flow and insert the value of it's properties into a database table.
The flow is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="10009" basePath="/ipay/bra" doc:name="HTTP Listener Configuration"/>
<mulexml:jaxb-context name="JAXB_Context" packageNames="com.dhg.api" doc:name="JAXB Context"/>
<flow name="transaction_initiation_testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/transaction" doc:name="HTTP">
<http:response-builder>
<http:header headerName="Content-Type" value="text/xml"/>
</http:response-builder>
</http:listener>
<mulexml:jaxb-xml-to-object-transformer returnClass="com.dhg.api.PAYMENTS" jaxbContext-ref="JAXB_Context" doc:name="XML to JAXB Object"/>
<logger message="#[payload.transaction.email]" level="INFO" doc:name="Logger"/>
<echo-component doc:name="Echo"/>
</flow>
</mule>
The object is as follows:
package com.dhg.api;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {"transaction"})
#XmlRootElement(name = "PAYMENTS")
public class PAYMENTS {
#XmlElement(name = "TRANSACTION", required = true)
protected PAYMENTS.TRANSACTION transaction;
public PAYMENTS.TRANSACTION getTRANSACTION() {
return transaction;
}
public void setTRANSACTION(PAYMENTS.TRANSACTION value) {
this.transaction = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"entity",
"useraddress1",
"useraddress2",
"useraddress3",
"usercountry",
"userparish",
"totalamount",
"payer",
"status",
"ipaynumber",
"email"
})
public static class TRANSACTION {
#XmlElement(name = "ENTITY", required = true)
protected PAYMENTS.TRANSACTION.ENTITY entity;
#XmlElement(name = "USERADDRESS1", required = true)
protected String useraddress1;
#XmlElement(name = "USERADDRESS2", required = true)
protected String useraddress2;
#XmlElement(name = "USERADDRESS3", required = true)
protected String useraddress3;
#XmlElement(name = "USERCOUNTRY", required = true)
protected String usercountry;
#XmlElement(name = "USERPARISH", required = true)
protected String userparish;
#XmlElement(name = "TOTALAMOUNT")
protected float totalamount;
#XmlElement(name = "PAYER", required = true)
protected String payer;
#XmlElement(name = "STATUS", required = true)
protected String status;
#XmlElement(name = "IPAYNUMBER")
protected int ipaynumber;
#XmlElement(name = "EMAIL", required = true)
protected String email;
#XmlAttribute(name = "txdate")
protected String txdate;
#XmlAttribute(name = "txno")
protected String txno;
public PAYMENTS.TRANSACTION.ENTITY getENTITY() {
return entity;
}
public void setENTITY(PAYMENTS.TRANSACTION.ENTITY value) {
this.entity = value;
}
public String getUSERADDRESS1() {
return useraddress1;
}
public void setUSERADDRESS1(String value) {
this.useraddress1 = value;
}
public String getUSERADDRESS2() {
return useraddress2;
}
public void setUSERADDRESS2(String value) {
this.useraddress2 = value;
}
public String getUSERADDRESS3() {
return useraddress3;
}
public void setUSERADDRESS3(String value) {
this.useraddress3 = value;
}
public String getUSERCOUNTRY() {
return usercountry;
}
public void setUSERCOUNTRY(String value) {
this.usercountry = value;
}
public String getUSERPARISH() {
return userparish;
}
public void setUSERPARISH(String value) {
this.userparish = value;
}
public float getTOTALAMOUNT() {
return totalamount;
}
public void setTOTALAMOUNT(float value) {
this.totalamount = value;
}
public String getPAYER() {
return payer;
}
public void setPAYER(String value) {
this.payer = value;
}
public String getSTATUS() {
return status;
}
public void setSTATUS(String value) {
this.status = value;
}
public int getIPAYNUMBER() {
return ipaynumber;
}
public void setIPAYNUMBER(int value) {
this.ipaynumber = value;
}
public String getEMAIL() {
return email;
}
public void setEMAIL(String value) {
this.email = value;
}
public String getTxdate() {
return txdate;
}
public void setTxdate(String value) {
this.txdate = value;
}
public String getTxno() {
return txno;
}
public void setTxno(String value) {
this.txno = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"account"
})
public static class ENTITY {
#XmlElement(name = "ACCOUNT", required = true)
protected PAYMENTS.TRANSACTION.ENTITY.ACCOUNT account;
#XmlAttribute(name = "biller")
protected String biller;
public PAYMENTS.TRANSACTION.ENTITY.ACCOUNT getACCOUNT() {
return account;
}
public void setACCOUNT(PAYMENTS.TRANSACTION.ENTITY.ACCOUNT value) {
this.account = value;
}
public String getBiller() {
return biller;
}
public void setBiller(String value) {
this.biller = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"details"
})
public static class ACCOUNT {
#XmlElement(name = "DETAILS", required = true)
protected PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS details;
#XmlAttribute(name = "bpnumber")
protected Integer bpnumber;
public PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS getDETAILS(){
return details;
}
public void setDETAILS(PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS value) {
this.details = value;
}
public Integer getBpnumber() {
return bpnumber;
}
public void setBpnumber(Integer value) {
this.bpnumber = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"dockey"
})
public static class DETAILS {
#XmlElement(name = "DOCKEY")
protected List<PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS.DOCKEY> dockey;
public List<PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS.DOCKEY> getDOCKEY() {
if (dockey == null) {
dockey = new ArrayList<PAYMENTS.TRANSACTION.ENTITY.ACCOUNT.DETAILS.DOCKEY>();
}
return this.dockey;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"value"
})
public static class DOCKEY {
#XmlValue
protected String value;
#XmlAttribute(name = "payment")
protected Float payment;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Float getPayment() {
return payment;
}
public void setPayment(Float value) {
this.payment = value;
}
}
}
}
}
}
}
After unmarshalling, the object is instantiated as com.dhg.api.PAYMENTS#some_random_string, which makes it difficult to reference. So my question is simply, how can I refer to this object and be able to access the values for use elsewhere in the flow.
Thanks for your help.
Your setter/getter is not compliant with property names in :
public PAYMENTS.TRANSACTION getTRANSACTION() {
return transaction;
}
public void setTRANSACTION(PAYMENTS.TRANSACTION value) {
this.transaction = value;
}
So in <logger message="#[payload.transaction.email]" level="INFO" doc:name="Logger"/> the transaction part is null.
The correct getter name is getTransaction and is the same for setter. Change them please

Android simpleXML Element '[element Name]' is already used

I have tried to use other problems brought up on this website, but none of them relate to what I am doing.
I am trying to 'deserialize' soap xml using simpleXML for an Android application. I'm getting closer and closer to getting this, but I have hit a brick wall here.
When I run my code, I get the following error:
org.simpleframework.xml.core.PersistenceException: Element 'serviceOrderCT' is already used with #org.simpleframework.xml.Element(name=, type=void, data=false, required=true) on field 'serviceOrderCT' private serviceOrderCT GetServiceOrdersResult.serviceOrderCT at line 26
So, the problem seems to be with the 'GetServiceOrdersResult.java' class (code below), but I can't put my finger on what it is.
I have been at this for hours and have gotten nowhere.
Help to get passed this problem would be appreciated.
Thanks.
[EDIT: Here's the XML File]
<GetServiceOrdersResponse xmlns="http://tempuri.org/">
<GetServiceOrdersResult xmlns:a="http://schemas.datacontract.org/2004/07/MobileWebService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:serviceOrderCT>
<a:_bill_to_Customer_No_>xxxxx</a:_bill_to_Customer_No_>
<a:_bill_to_Name>xxxxxxx</a:_bill_to_Name>
<a:_customer_No_ i:nil="true" />
<a:_description />
<a:_fix_By_Date>xxxxxxxxxxxxx</a:_fix_By_Date>
<a:_fix_By_Time />
<a:_fixed_Date>xxxxxxxxxxxx</a:_fixed_Date>
<a:_fixed_Time>xxxxxxxxxxxxxx</a:_fixed_Time>
<a:_name>xxxxxxxxxxxxxxx</a:_name>
<a:_no_>xxxxxxxxxxxxxxxxxxx</a:_no_>
<a:_order_Date>xxxxxxxxxxxxxxx</a:_order_Date>
<a:_order_Time>xxxxxxxxxxxxx</a:_order_Time>
<a:_responded_Date>xxxxxxxxxxxxxxxxxx</a:_responded_Date>
<a:_responded_Time />
<a:_response_Date>xxxxxxxxxxxxxxxxxx</a:_response_Date>
<a:_response_Time>xxxxxxxxxxxxxxxxxxxx</a:_response_Time>
<a:_transaction_Status>xxxxxxxxxxxxxxx</a:_transaction_Status>
<a:_your_Reference />
</a:serviceOrderCT>
<a:serviceOrderCT>
a:_bill_to_Customer_No_>xxxxx</a:_bill_to_Customer_No_>
<a:_bill_to_Name>xxxxxxx</a:_bill_to_Name>
<a:_customer_No_ i:nil="true" />
<a:_description />
<a:_fix_By_Date>xxxxxxxxxxxxx</a:_fix_By_Date>
<a:_fix_By_Time />
<a:_fixed_Date>xxxxxxxxxxxx</a:_fixed_Date>
<a:_fixed_Time>xxxxxxxxxxxxxx</a:_fixed_Time>
<a:_name>xxxxxxxxxxxxxxx</a:_name>
<a:_no_>xxxxxxxxxxxxxxxxxxx</a:_no_>
<a:_order_Date>xxxxxxxxxxxxxxx</a:_order_Date>
<a:_order_Time>xxxxxxxxxxxxx</a:_order_Time>
<a:_responded_Date>xxxxxxxxxxxxxxxxxx</a:_responded_Date>
<a:_responded_Time />
<a:_response_Date>xxxxxxxxxxxxxxxxxx</a:_response_Date>
<a:_response_Time>xxxxxxxxxxxxxxxxxxxx</a:_response_Time>
<a:_transaction_Status>xxxxxxxxxxxxxxx</a:_transaction_Status>
<a:_your_Reference />
</a:serviceOrderCT>
</GetServiceOrdersResult>
</GetServiceOrdersResponse>
Envelope.java
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
#Root
public class Envelope {
#Element(name = "Body")
private Body body;
public Body getBody() {
return body;
}
}
Body.java
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import java.util.List;
public class Body {
#Element(name = "GetServiceOrdersResponse")
private GetServiceOrdersResponse getServiceOrdersResponse;
public GetServiceOrdersResponse getServiceOrdersResponse() {
return getServiceOrdersResponse;
}
}
GetServiceOrdersResponse.java
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
public class GetServiceOrdersResponse {
#Element(name = "GetServiceOrdersResult")
private GetServiceOrdersResult getServiceOrdersResult;
public GetServiceOrdersResult getGetServiceOrdersResult() {
return getServiceOrdersResult;
}
}
GetServiceOrdersResult.java
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import java.util.List;
public class GetServiceOrdersResult {
#Element
private serviceOrderCT serviceOrderCT;
public serviceOrderCT getServiceOrderCT() {
return serviceOrderCT;
}
}
serviceOrderCT
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
public class serviceOrderCT {
#Element(name = "_bill_to_Customer_No_", required = false)
private String billToCustomerNo;
#Element(name = "_bill_to_Name", required = false)
private String billToName;
#Element(name = "_customer_No_", required = false)
private String customerNo;
#Element(name = "_description", required = false)
private String description;
#Element(name = "_fix_By_Date", required = false)
private String fixByDate;
#Element(name = "_fix_By_Time", required = false)
private String fixByTime;
#Element(name = "_fixed_Date", required = false)
private String fixedDate;
#Element(name = "_fixed_Time", required = false)
private String fixedTime;
#Element(name = "_name", required = false)
private String name;
#Element(name = "_no_", required = false)
private String no;
#Element(name = "_order_Date", required = false)
private String orderDate;
#Element(name = "_order_Time", required = false)
private String orderTime;
#Element(name = "_responded_Date", required = false)
private String respondedDate;
#Element(name = "_responded_Time", required = false)
private String respondedTime;
#Element(name = "_response_Date", required = false)
private String responseDate;
#Element(name = "_response_Time", required = false)
private String responseTime;
#Element(name = "_transaction_Status", required = false)
private String transactionStatus;
#Element(name = "_your_Reference", required = false)
private String yourReference;
public serviceOrderCT() {
}
public String getBillToCustomerNo() {
return billToCustomerNo;
}
public void setBillToCustomerNo(String billToCustomerNo) {
this.billToCustomerNo = billToCustomerNo;
}
public String getBillToName() {
return billToName;
}
public void setBillToName(String billToName) {
this.billToName = billToName;
}
public String getCustomerNo() {
return customerNo;
}
public void setCustomerNo(String customerNo) {
this.customerNo = customerNo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFixByDate() {
return fixByDate;
}
public void setFixByDate(String fixByDate) {
this.fixByDate = fixByDate;
}
public String getFixByTime() {
return fixByTime;
}
public void setFixByTime(String fixByTime) {
this.fixByTime = fixByTime;
}
public String getFixedDate() {
return fixedDate;
}
public void setFixedDate(String fixedDate) {
this.fixedDate = fixedDate;
}
public String getFixedTime() {
return fixedTime;
}
public void setFixedTime(String fixedTime) {
this.fixedTime = fixedTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getOrderDate() {
return orderDate;
}
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
public String getOrderTime() {
return orderTime;
}
public void setOrderTime(String orderTime) {
this.orderTime = orderTime;
}
public String getRespondedDate() {
return respondedDate;
}
public void setRespondedDate(String respondedDate) {
this.respondedDate = respondedDate;
}
public String getRespondedTime() {
return respondedTime;
}
public void setRespondedTime(String respondedTime) {
this.respondedTime = respondedTime;
}
public String getResponseDate() {
return responseDate;
}
public void setResponseDate(String responseDate) {
this.responseDate = responseDate;
}
public String getResponseTime() {
return responseTime;
}
public void setResponseTime(String responseTime) {
this.responseTime = responseTime;
}
public String getTransactionStatus() {
return transactionStatus;
}
public void setTransactionStatus(String transactionStatus) {
this.transactionStatus = transactionStatus;
}
public String getYourReference() {
return yourReference;
}
public void setYourReference(String yourReference) {
this.yourReference = yourReference;
}
}
Main.java
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.File;
public class Main {
public static void main(String[] args) {
try {
Serializer serializer = new Persister();
File result = new File("C:\\Users\\xxxxxxxx\\Documents\\file.xml");
Envelope example = serializer.read(Envelope.class, result);
System.out.println(example.getBody().getServiceOrdersResponse().getGetServiceOrdersResult().getServiceOrderCT().getBillToCustomerNo());
} catch (Exception e) {
e.printStackTrace();
}
}
}
So, I solve my problem, a day after asking for help, on my own Yet again.
You have to implement an ElementList, if you want to get all three serviceOrderCTs. It was only picking up one.
If I removed the other two, I would get something back.
Advice for anyone who reads this, stick with the official documentation. It will help you a lot. http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list

JAXB Moxy Element inside Text Content

I have to un-/marshall the following snippet
<para sizeInfoId="sizeInfo2" styleId="mono">Franz jagt im komplett verwahrlosten <protectedText>Taxi</protectedText> quer durch Bayern.</para>
My Java Model looks like this
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
#XmlType(propOrder = { AcrossParagraph.XML_ID, AcrossParagraph.XML_STYLE_ID, AcrossParagraph.XML_SIZEINFO_ID, AcrossParagraph.XML_COMMENT, AcrossParagraph.XML_CONTENT })
#XmlRootElement(name = AcrossParagraph.XML_ROOT_TAG)
public class AcrossParagraph {
public static final String XML_ROOT_TAG = "para";
public static final String XML_ID = "id";
public static final String XML_STYLE_ID = "styleId";
public static final String XML_SIZEINFO_ID = "sizeInfoId";
public static final String XML_COMMENT = "comment";
public static final String XML_CONTENT = "content";
private String id;
private String styleId;
private String sizeInfoId;
private String comment;
private String content;
#XmlAttribute(name = AcrossParagraph.XML_ID)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#XmlAttribute(name = AcrossParagraph.XML_STYLE_ID)
public String getStyleId() {
return styleId;
}
public void setStyleId(String styleId) {
this.styleId = styleId;
}
#XmlTransient
public void setStyleId(AcrossStyle style) {
this.styleId = style.getId();
}
#XmlAttribute(name = AcrossParagraph.XML_SIZEINFO_ID)
public String getSizeInfoId() {
return sizeInfoId;
}
public void setSizeInfoId(String sizeInfoId) {
this.sizeInfoId = sizeInfoId;
}
#XmlTransient
public void setSizeInfoId(AcrossSize size) {
this.sizeInfoId = size.getId();
}
#XmlAttribute(name = AcrossParagraph.XML_COMMENT)
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
#XmlValue
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Without the inner protectedText Tag everything is working, but I don't know how to map that inner element.
I have read something about XmlAnyElement Annotation but havn't found an example for mapping something like this.
Any ideas?
Best regards,
Pascal
create new class for protectedText element:
#XmlRootElement(name = "protectedText")
class ProtectedText implements Serializable{
#XmlValue
public String value;
}
now change content property in AcrossParagraph as below:
private List<Serializable> content;
#XmlElementRef(name = "protectedText", type = ProtectedText.class)
#XmlMixed
public List<Serializable> getContent(){
return content;
}
public void setContent(List<Serializable> content){
this.content = content;
}
when you unmarshal the content list contains mix of String and ProtectedText

Java xml serialization null handling

I am using JAXB2 to serialize object to xml.
Is there any way how to force it to create entire object structure like in following example even if it is not filled in backing object?
This is my intended result even without having asignee property set.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<note>
<to xsi:nil="true"/>
<from xsi:nil="true"/>
<header xsi:nil="true"/>
<body>text</body>
<assignee>
<name xsi:nil="true"/>
<surname xsi:nil="true"/>
</assignee>
</note>
I use following code for serialization:
JAXBContext jc = JAXBContext.newInstance(dataObject.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
marshaller.marshal(dataObject, outputStream);
You can do the following:
Note
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement
#XmlType(propOrder={"to", "from", "header", "body", "assignee"})
public class Note {
private String to;
private String from;
private String header;
private String body;
private Assignee assignee;
#XmlElement(nillable=true)
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
#XmlElement(nillable=true)
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
#XmlElement(nillable=true)
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
#XmlElement(nillable=true)
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Assignee getAssignee() {
return assignee;
}
public void setAssignee(Assignee assignee) {
this.assignee = assignee;
}
}
Assignee
We will need to have a means to no when an unmarshalled instance of Assignee should be interpreted as null. I have added an isNull() method that returns true if all the fields are null.
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlJavaTypeAdapter(AssigneeAdapter.class)
public class Assignee {
private String name;
private String surname;
#XmlElement(nillable=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement(nillable=true)
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public boolean isNull() {
return null == name && null == surname;
}
}
AssigneeAdapter
The AssigneeAdapter uses both the Assignee object for the value type and bound type. This class leverages the isNull() method we added on Assignee:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AssigneeAdapter extends XmlAdapter<Assignee, Assignee> {
#Override
public Assignee unmarshal(Assignee v) throws Exception {
if(v.isNull()) {
return null;
}
return v;
}
#Override
public Assignee marshal(Assignee v) throws Exception {
if(null == v) {
return new Assignee();
}
return v;
}
}
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(Note.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(new Note(), System.out);
}
}
For more information on XmlAdapter see:
http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
Yes. Use a combination of #XmlElementRef and JAXBElement with nil set to true.
See:
http://download.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBElement.html
http://download.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElementRef.html

Categories