I'm working with the ECCP protocol in order to integrate my CRM with the Elastix Call Center Module. The protocol uses a XML structure defined as follows:
<request id="1">
<request_type> <!-- this will be mapped to the Java request class -->
<attributes>
</attributes>
</request_type>
</request>
and
<response id="1">
<response_type> <!-- this will be mapped to the Java response class -->
<attributes>
</attributes>
</response_type>
</response>
I'm using JAX-B to map XML to Java classes but the problem is that I have to put the JAX-B generated XML inside a <request></request> XML every request and extract the content from <response></response> in every response because the ECCP protocol defines that every request and response needs to nested to their respective elements.
Here's the code I'm using to do that:
document = createDocument();
Element requestWrapper = document.createElement("request");
requestWrapper.setAttribute("id", String.valueOf(wrapped.getId()));
document.appendChild(requestWrapper);
JAXBContext jc = JAXBContext.newInstance(wrapped.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(wrapped, requestWrapper);
Exemplifying:
One of ECCP's protocol operation is JAX-B-mapped into a class like this (getters and setters were omitted):
#XmlRootElement(name = "loginagent")
#XmlAccessorType(XmlAccessType.FIELD)
public class EccpLoginAgentRequest implements IEccpRequest {
#XmlElement(name = "agent_number")
private String agentNumber;
#XmlElement(name = "password")
private String password;
}
And JAX-B outputs the following:
<loginagent>
<agent_number>username</agent_number>
<password>password</password>
</loginagent>
But what the ECCP's protocol requires is:
<request id="1"> <!-- id is an auto-increment number to identify the request -->
<loginagent>
<username>username</username>
<password>password</password>
</loginagent>
</request>
The question is: is there any other way to achieve in any other better way?
Thank you.
You can probably check out the #XmlSeeAlso annotation which will help you wrap the same content both for request and response. For the inner part you can create the separate class and map all the fields appropriately. I hope this helps you a little bit.
EDIT:
Sorry for the long response time. You need to create a wrapper class with the inner structure defined as an #XmlElement. Here's the way to achieve that XML structure:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "request")
public class RequestWrapper {
#XmlElement(name = "loginagent", required = true)
protected EccpLoginAgentRequest loginagent;
public EccpLoginAgentRequest getLoginagent() {
return loginagent;
}
public void setLoginagent(EccpLoginAgentRequest loginagent) {
this.loginagent = loginagent;
}
}
And here's the EccpLoginAgentRequest structure:
#XmlAccessorType(XmlAccessType.FIELD)
public class EccpLoginAgentRequest {
#XmlElement(name = "agent_number")
private String agentNumber;
#XmlElement(name = "password")
private String password;
// getters and setters omitted
}
So in result, you can print the XML you want like that:
JAXBContext jaxbContext = JAXBContext.newInstance(Wrapper.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
EccpLoginAgentRequest request = new EccpLoginAgentRequest();
request.setAgentNumber("1");
request.setPassword("pass");
Wrapper wrapper = new Wrapper();
wrapper.setLoginagent(request);
jaxbMarshaller.marshal(wrapper, System.out);
It will give you the following output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
<loginagent>
<agent_number>1</agent_number>
<password>pass</password>
</loginagent>
</request>
I found a way to solve this in this post: XML element with attribute and content using JAXB
So I've mapped a EccpRequestWrapper object as the following:
#XmlRootElement(name = "request")
public class EccpRequestWrapper {
#XmlAttribute
private Long id;
#XmlAnyElement
private IEccpRequest request;
}
and then my request JAX-B outputs my request the way ECCP protocol requires.
The #XmlAttribute and #XmlAnyElement annotation did the trick.
<request id="1">
<login>
<username>user</username>
<password>****</password>
</login>
</request>
A good JAXB guide can be found here https://jaxb.java.net/guide/Mapping_interfaces.html
Related
I have a batch job that retrieves records from a database, processes them, and passes the result into the writer. The bean passed to the writer has four fields that need to be written to separate xml files. One of the fields is a bean representing the original record, and the other three fields are collections of child elements associated with the record.
I initially tried to use jackson to parse the beans and generate the files, but I found that approach had trouble when applied to the batch model.
Next, I've shifted to using StaxEventItemWriters for each child field, which individually seem perfectly adequate, but I'm having trouble implementing a writer that can handle all the various sub-types. I've looked into the CompositeItemWriter and ClassifierCompositeItemWriter, but they seem more suited to having multiple writers for the same type of bean, whereas I need multiple writers appropriate for differing types. Any advice would be greatly appreciated!
Domain example:
public class MyBean {
private RecordBean recordBean;
private List<ChildTypeA> aBeans;
private List<ChildTypeB> bBeans;
private List<ChildTypeC> cBeans;
}
#XmlRootElement(name = "RECORD")
public class RecordBean extends MyAbstractBean {
#XmlElement(name = "ID")
private String recordId;
#XmlElementWrapper(name = "A_CHILDREN")
#XmlElement(name="CHILD_TYPE_A")
List<Long> aChildIds}
#XmlElementWrapper(name = "B_CHILDREN")
#XmlElement(name="CHILD_TYPE_B")
List<Long> bChildIds}
#XmlElementWrapper(name = "C_CHILDREN")
#XmlElement(name="CHILD_TYPE_C")
List<Long> cChildIds}
}
#XmlRootElement(name = "CHILD_TYPE_A")
public class ChildTypeA extends MyAbstractBean {
#XmlElement(name = "ID") private String aId;
}
#XmlRootElement(name = "CHILD_TYPE_B")
public class ChildTypeB extends MyAbstractBean {
#XmlElement(name = "ID") private String bId;
}
#XmlRootElement(name = "CHILD_TYPE_C")
public class ChildTypeC extends MyAbstractBean {
#XmlElement(name = "ID") private String cId;
}
For each container bean passed to the writer, I need to create a unique XML file for each RecordBean e.g. record_1.xml, and I need to write each collection into an aggregate file that will serve as a library of all children for that child type, across all the records.
Output example:
record_1.xml
<?xml version="1.0" encoding="UTF-8"?>
<RECORD>
<ID>1</ID>
<A_CHILDREN>
<CHILD_TYPE_A>1</CHILD_TYPE_A>
<CHILD_TYPE_A>2</CHILD_TYPE_A>
</A_CHILDREN>
<B_CHILDREN>
<CHILD_TYPE_B>1</CHILD_TYPE_B>
<CHILD_TYPE_B>2</CHILD_TYPE_B>
</B_CHILDREN>
<A_CHILDREN>
<CHILD_TYPE_C>1</CHILD_TYPE_C>
<CHILD_TYPE_C>2</CHILD_TYPE_C>
</A_CHILDREN>
</RECORD>
</xml>
record_2.xml
<?xml version="1.0" encoding="UTF-8"?>
<RECORD>
<ID>2</ID>
<A_CHILDREN>
<CHILD_TYPE_A>3</CHILD_TYPE_A>
<CHILD_TYPE_A>4</CHILD_TYPE_A>
</A_CHILDREN>
<B_CHILDREN>
<CHILD_TYPE_B>3</CHILD_TYPE_B>
<CHILD_TYPE_B>4</CHILD_TYPE_B>
</B_CHILDREN>
<A_CHILDREN>
<CHILD_TYPE_C>3</CHILD_TYPE_C>
<CHILD_TYPE_C>4</CHILD_TYPE_C>
</A_CHILDREN>
</RECORD>
</xml>
a_children.xml
<?xml version="1.0" encoding="UTF-8"?>
<A_CHILDREN>
<CHILD_TYPE_A>
<ID>1</ID>
</CHILD_TYPE_A>
<CHILD_TYPE_A>
<ID>2</ID>
</CHILD_TYPE_A>
<CHILD_TYPE_A>
<ID>3</ID>
</CHILD_TYPE_A>
<CHILD_TYPE_A>
<ID>4</ID>
</CHILD_TYPE_A>
</A_CHILDREN>
</xml>
<!-- Decide which format to use -->
b_children.xml & c_children.xml are the same as a_children.xml.
So after the better part of two days, I decided to take a different approach. I think it would be possible to create a set of writers to handle the task, but I think it would require a fair amount of customization, and a complex hierarchy of delegation.
For example:
CompositeItemWriter delegates to:
ClassifierCompositeItemWriter classifies, and delegates to:
StaxEventItemWriter for ChildTypeA
StaxEventItemWriter for ChildTypeB
StaxEventItemWriter for ChildTypeC
MultiResourceItemWriter Dynamically assigns FilesystemResource for each RecordBean.
Despite the fact that it probably could be done, I realized that it's just simpler to write the XML as strings wherever I want. Something along the lines of:
#Component
#Scope("step")
public class MyXmlWriter implements ItemWriter<MyBean> {
#Override
public void write(List<? extends MyBean> beans) throws Exception {
for(MyBean bean : beans) {
this.writeRecordBean(bean.getRecordBean());
bean.getAChildIds().forEach(a -> this.writeElement(a, pathA));
bean.getBChildIds().forEach(b -> this.writeElement(b, pathB));
bean.getCChildIds().forEach(c -> this.writeElement(c, pathC));
}
}
private void writeElement(Long Id, Path path) {
// Handle serialization and FileIO here
}
.
.
.
}
Anyway, I hope this helps anyone digging into the Spring Batch weeds like I was. Cheers!
If I have a XML with huge header tags but I need to get only the list of objects dppc and child object ppc. Please advise how to get the values only from the node dppc.
<SOAPENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAPENV:Body>
<rpc:distributeObject xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rpc="http://company.xxx.com/Distributed/Object">
<standardHeader xmlns="http://wsi.nat.bat.com/2005/06/StandardHeader/">
<dppc>
<ppc>
<productName>Export1</productName>
</ppc>
<ppc>
<productName>Export2</productName>
</ppc>
</dppc>
</standardHeader>
</rpc:distributeObject>
</SOAPENV:Body>
Please find the below code to unmarshal it
String example =
"<SOAPENV:Envelope xmlns:SOAPENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAPENV:Body><rpc:distributeObject xmlns:SOAPENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:rpc=\"http://company.xxx.com/Distributed/Object\"><standardHeader xmlns=\"http://wsi.nat.bat.com/2005/06/StandardHeader/\"><dppc><ppc><productName>Export1</productName></ppc><ppc><productName>Export2</productName></ppc></dppc></standardHeader></rpc:distributeObject></SOAPENV:Body>";
message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(example.getBytes()));
Unmarshaller unmarshaller = JAXBContext.newInstance(Dppc.class).createUnmarshaller();
Dppc dppc = (Dppc)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
dppc.getPPC();
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
class Dppc{
#XmlPath("rpc:distributeObject/standardHeader/dppc")
private List<PPC> ppC;
public List<PPC> getPPC() {
return ppC;
}
public void setPPC(List<PPC> ppC) {
this.ppC= ppC;
}
}
class PPC {
String productName;
//getter & setters;
}
I have created a package-info file also, but it is not working.
With the pure Implementation of JAXB this is not possible because JAXB don't support XPath by default.
One possible solution is to use EclipseLink(MOXy) which supports the #XmlPath Annotation.
With that annotation it is possible to define the Path of an Element inside the XML. For your XML to read only the dppc Elements and childs your class would look something like this (not tested):
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="SOAPENV:Body")
public class MainElement{
#XmlPath("rpc:distributeObject/standardHeader/dppc")
DPPC dppc;
//getter and setters
}
Same for the dppc and ppc class.
The Link to MOXy give you some nice examples.
Given something like:
<root>
<wrapper>
<wrapped id="..."/>
<wrapped id="..."/>
</wrapper>
</root>
how can I map it to this POJO:
public class Root {
public Set<UUID> myIds = new LinkedHashSet();
}
I am wondering since #XmlElement( name = "wrapped" ) #XmlElementWrapper( name = "wrapper" ) works somewhat similar to what I want, is there something to get the attribute?
note: i am not using moxy so as far as I know, I cannot use xpaths. I am trying to avoid the #XmlJavaTypeAdapter route.
You need to modify your root class a little bit,
so that it will contain a Set<Wrapped> instead of a Set<UUID>.
#XmlRootElement(name = "root")
public class Root {
#XmlElementWrapper(name = "wrapper")
#XmlElement(name = "wrapped")
public Set<Wrapped> myWrappeds = new LinkedHashSet<>();
}
And you need a separate class for the <wrapped> elements.
Surprisingly for me, you don't need an #XmlJavaAdapter for id here, because JAXB already has a built-in converter between java.util.UUID and String.
public class Wrapped {
#XmlAttribute(name = "id")
public UUID id;
}
I have checked the above with this XML input file
<root>
<wrapper>
<wrapped id="550e8400-e29b-11d4-a716-446655440000"/>
<wrapped id="550e8400-e29b-11d4-a716-446655440001"/>
</wrapper>
</root>
and this main method which reproduces the original XML:
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("root.xml");
Root root = (Root) unmarshaller.unmarshal(file);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
I have the following XML that I am trying to unmarshal:
<ListOfYear>
<Requests/>
<Payload>
<Year>
<Value>2013</Value>
</Year>
<Year>
<Value>2012</Value>
</Year>
<Year>
<Value>2011</Value>
</Year>
<Year>
<Value>2010</Value>
</Year>
<Year>
<Value>2009</Value>
</Year>
<Year>
<Value>2008</Value>
</Year>
</Payload>
</ListOfYear>
My java class looks like this:
#XmlRootElement(name = "ListOfYear")
public class YearOutput {
#XmlElementWrapper(name = "Payload")
#XmlElement(name = "Year")
private ArrayList<Year> Payload;
public ArrayList<Year> getPayload() {
return Payload;
}
public void setPayload(ArrayList<Year> payload) {
Payload = payload;
}
}
Payload should contain a list of year objects:
#XmlRootElement(name = "Year")
#XmlAccessorType(XmlAccessType.FIELD).
public class Year {
#XmlElement(name = "Value")
int Value;
public int getValue() {
return Value;
}
public void setValue(int value) {
Value = value;
}
}
I am unmarshaling the XML with the following code:
String r = response.getEntity(String.class);
JAXBContext jaxbContext = JAXBContext.newInstance(YearOutput.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
YearOutput output = (YearOutput) unmarshaller.unmarshal(new StringReader(r));
I get an output object back just fine, the payload object is always null. I have tried several different approaches with my XML annotation and have not been able to get anything to work. Any help would be much appreciated.
EDIT
I thought the name space was inconsequential so I didn't post that part of the code. But #Blaise Doughan suggested I marshal my model and it appears my namespace may be causing an issue...
Here is the full XML that I need:
<ListOfYear xmlns="http://something.com/">
<Requests/>
<Payload>
<Year>
<Value>2013</Value>
</Year>
<Year>
<Value>2012</Value>
</Year>
<Year>
<Value>2011</Value>
</Year>
<Year>
<Value>2010</Value>
</Year>
<Year>
<Value>2009</Value>
</Year>
<Year>
<Value>2008</Value>
</Year>
</Payload>
</ListOfYear>
Here is my full model:
#XmlRootElement(name = "ListOfYear", namespace = "http://something.com/")
public class YearOutput {
#XmlElementWrapper(name = "Payload")
#XmlElement(name = "Year")
private ArrayList<Year> Payload;
public ArrayList<Year> getPayload() {
return Payload;
}
public void setPayload(ArrayList<Year> payload) {
Payload = payload;
}
}
Now when I marshal my model I am getting:
<?xml version="1.0" encoding="UTF-8"?>
<ns2:ListOfYear xmlns:ns2="https://something.com/">
<Payload>
<Year>
<Value>2008</Value>
</Year>
</Payload>
</ns2:ListOfYear>
So what am I doing wrong with my namespace?
EDIT
After adding package-info.java everything works perfect!
#XmlSchema(
namespace = "https://something.com/",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Your annotations need to go on the property (get or set method) instead of the field. If you wish to have them on the field (instance variable), the you need to annotate your class with #XmlAccessorType(XmlAccessType.FIELD), like you have on your Year class.
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
UPDATE
Instead of specifying the namespace on #XmlRootElement you will need to leverage a package level #XmlSchema annotation (on a class called package-info) to match the namespace qualification.
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
Try adding a / to the ending Payload tag.
A it's already mentioned, the XML file should contain
</Payload>
instead of
<Payload>
in the last but one line
#XmlElement should be above attribute but not getter in Year class:
#XmlElement int Value;
The element name in XmlElement annotation on getValue is missing. JAXB default behavior is that getValue means 'value' element, not 'Value'. Add the annotation #XmlElement(name="Value")
Fix it as in your other code
#XmlRootElement(name = "Year")
public static Year {
...
#XmlElement(name="Value")
public int getValue() {
return Value;
}
...
}
If you also marshal I recommend to add #XmlAccessorType(XmlAccessType.NONE) to the classes to prevent adding both 'value' and 'Value' elements.
See full working code at package 'wicketstuff/enteam/wicket/examples14/mixed' on
https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-1.4/browse/
Also a test that log into console your XML and parse it back is provided in 'YearOutputTest'
I am trying to use a XML and access to all fields and data on an easy way, so, I decided to use JaxB , but I have no idea how to create all the classes for the objects, I tried manually like this.
#XmlRootElement(name = "Response")
public class Response {
#XmlElement(ns = "SignatureValue")
String signatureValue;
}
But I get an error on #XmlElement saying the annotation is disallowed for this location...
I checked other posts and they work great if I have something like Hellw but doesnt work with more complex formats, an example of first part of mine is like this
<?xml version="1.0" encoding="UTF-8"?><DTE xsi:noNamespaceSchemaLocation="http://www.myurl/.xsd" xmlns:gs1="urn:ean.ucc:pay:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
any idea how to do all this??
Thanks in advance
EDIT:
I forgot to say, the XML is actually a String with the entire XML.
The #XmlElement annotation is valid on a field. If you have a corresponding property then you should annotate the class with #XmlAccessorType(XmlAccessType.FIELD) to avoid a duplicate mapping exception.
Java Model
Annotating the Field
#XmlRootElement(name = "Response")
#XmlAccessorType(XmlAccessType.FIELD)
public class Response {
#XmlElement(name = "SignatureValue")
String signatureValue;
public String getSignatureValue() {
return signatureValue;
}
public void setSignatureValue(String signatureValue) {
this.signatureValue = signatureValue;
}
}
Annotating the Property
import javax.xml.bind.annotation.*;
#XmlRootElement(name = "Response")
public class Response {
String signatureValue;
#XmlElement(name = "SignatureValue")
public String getSignatureValue() {
return signatureValue;
}
public void setSignatureValue(String signatureValue) {
this.signatureValue = signatureValue;
}
}
For More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
Demo Code
Below is some demo code that reads/writes the XML corresponding to your Response class.
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum19713886/input.xml");
Response response = (Response) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(response, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<SignatureValue>Hello World</SignatureValue>
</Response>