Consider this example -
I have a class called Report that has a field of type Message. The Message class has a field called "body" which is a string. "body" can be any string, but sometimes it contains properly formatted XML content. How can I ensure that when the "body" contains XML content, the serialization takes the form of an XML structure rather than what it gives at present?
Here is the code with the output -
Report class
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement(name = "Report")
#XmlType(propOrder = { "message"})
public class Report
{
private Message message;
public Message getMessage() { return message; }
public void setMessage(Message m) { message = m; }
}
Message class
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = { "body" })
public class Message
{
private String body;
public String getBody() { return body; }
#XmlElement
public void setBody(String body) { this.body = body; }
}
Main
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class SerializationTest
{
public static void main(String args[]) throws Exception
{
JAXBContext jaxbContext = JAXBContext.newInstance(Report.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Report report = new Report();
Message message = new Message();
message.setBody("Sample report message.");
report.setMessage(message);
jaxbMarshaller.marshal(report, System.out);
message.setBody("<rootTag><body>All systems online.</body></rootTag>");
report.setMessage(message);
jaxbMarshaller.marshal(report, System.out);
}
}
The output is as follows -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Report>
<message>
<body>Sample report message.</body>
</message>
</Report>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Report>
<message>
<body><rootTag><body>All systems online.</body></rootTag></body>
</message>
</Report>
As you can see in the above output, for the second instance of "body", the serialization produced
<body><rootTag><body>All systems online.</body></rootTag></body>
instead of
<body><rootTag><body>All systems online.</body></rootTag></body>
How to solve this problem?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
This use case is mapped using the #XmlAnyElement annotation and specifying a DOMHandler. There appears to be bug when doing this with the JAXB RI, but the following use case works with EclipseLink JAXB (MOXy).
BodyDomHandler
By default a JAXB impleemntation will represent unmapped content as a DOM node. You can leverage a DomHandler to an alternate representation of the DOM, In this case we will represent the DOM as a String.
import java.io.*;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.DomHandler;
import javax.xml.transform.Source;
import javax.xml.transform.stream.*;
public class BodyDomHandler implements DomHandler<String, StreamResult> {
private static final String BODY_START_TAG = "<body>";
private static final String BODY_END_TAG = "</body>";
private StringWriter xmlWriter = new StringWriter();
public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
return new StreamResult(xmlWriter);
}
public String getElement(StreamResult rt) {
String xml = rt.getWriter().toString();
int beginIndex = xml.indexOf(BODY_START_TAG) + BODY_START_TAG.length();
int endIndex = xml.indexOf(BODY_END_TAG);
return xml.substring(beginIndex, endIndex);
}
public Source marshal(String n, ValidationEventHandler errorHandler) {
try {
String xml = BODY_START_TAG + n.trim() + BODY_END_TAG;
StringReader xmlReader = new StringReader(xml);
return new StreamSource(xmlReader);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
Message
Below is how you would specify the #XmlAnyElement annotation on your Message class.
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = { "body" })
public class Message
{
private String body;
public String getBody() { return body; }
#XmlAnyElement(BodyDomHandler.class)
public void setBody(String body) { this.body = body; }
}
Output
Below is the output from running your SerialziationTest:
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<message>
<body>Sample report message.</body>
</message>
</Report>
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<message>
<body>
<rootTag>
<body>All systems online.</body>
</rootTag>
</body>
</message>
</Report>
For More Information
http://blog.bdoughan.com/2011/04/xmlanyelement-and-non-dom-properties.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
NOTE - Bug in JAXB RI
There appears to be a bug in the JAXB reference implementation, and the example code will result in a stack trace like the following:
Exception in thread "main" javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at forum12428727.SerializationTest.main(SerializationTest.java:20)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:216)
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:100)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:664)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:561)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:290)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:462)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314)
... 3 more
If its only for Marshalling, and to ignore the < and >,
We can use the following:
marshaller.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
new CharacterEscapeHandler() {
#Override
public void escape(char[] ac, int i, int j, boolean flag,
Writer writer) throws IOException {
writer.write(ac, i, j);
}
});
3 different solutions 1), 2) 3), here below :
1) Following post is a the description of your solution Loresh :
http://anna-safronova.livejournal.com/2524.html?thread=9180
This is still missing limitations details.
With embeeded html, we need a <![CDATA block
JAXB's dependancy
com.sun.xml.bind.marshaller.CharacterEscapeHandler
Needs import jaxb-impl for compilation / and may be required for excution, e.g.
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.4</version>
Limitation : this solution is Container-specific and may not run because of class-loading policy.
2) Another similar approach is JDK's rt.jar dependancy
com.sun.xml.internal.bind.CharacterEscapeHandler
http://theopentutorials.com/tutorials/java/jaxb/jaxb-marshalling-and-unmarshalling-cdata-block/
Same limitation / dependends on target JDK, and some tweaks on Eclipse/Maven are necessary (bad alternative / My opinion)
3) Finally, the best solution was found on another post of Reg Whitton :
https://stackoverflow.com/a/12637295/560410
and this is the detailed reciepe :
http://javacoalface.blogspot.co.uk/2012/09/outputting-cdata-sections-with-jaxb.html
Worked perfect for me !
Related
I have a XML like below:
<content>
<p><b>Node:</b> Some information</p>
</content>
When deserializing this XML, I want to get the content inside p tag as a string.
For example, if I have a java class like below:
#Data
class Content {
TextInParagraph p;
}
#Data
class TextInParagraph {
String text;
}
I should have value of text as "<b>Node:</b> Some information".
Is there a way I can do above using JAXB or Jackson XML parser?
I tried deserializing above in Jackson, but I am getting below exception:
Expected END_ELEMENT, got event of type 1
java.io.IOException: Expected END_ELEMENT, got event of type 1
Sadly, this is not possible with jackson-dataformat-xml.
With JAXB however you can solve this by using a DomHandler
#XmlRootElement(name = "content")
#XmlAccessorType(XmlAccessType.FIELD)
public class Content {
#XmlAnyElement(InnerXmlHandler.class)
private String p;
}
DomHandler
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.DomHandler;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
public class InnerXmlHandler implements DomHandler<String, StreamResult> {
private static final String START_TAG = "<p>";
private static final String END_TAG = "</p>";
private StringWriter xmlWriter = new StringWriter();
public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
return new StreamResult(xmlWriter);
}
public String getElement(StreamResult rt) {
String xml = rt.getWriter().toString();
int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
int endIndex = xml.lastIndexOf(END_TAG);
return xml.substring(beginIndex, endIndex);
}
public Source marshal(String n, ValidationEventHandler errorHandler) {
try {
String xml = START_TAG + n.trim() + END_TAG;
StringReader xmlReader = new StringReader(xml);
return new StreamSource(xmlReader);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
This works with the sample you provided, but even works with nested <p> tags like:
<content>
<p> This is some <ul><li>list</li></ul> and <p>nested paragraph</p></p>
</content>
However, this works only when the inner HTML/XML is valid. The following will not work and throw an exception like The element type "ul" must be terminated by the matching end-tag "</ul>".
<content>
<p> This is some <ul>invalid xml </p>
</content>
This is because of JAXBs internals which traverses all inner elements although the dom handler is provided.
I am trying to convert a Pojo to XML using the JAXB library.
I need the end result to look something like this:
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<!--other stuff-->
</soap:Body>
</soap:Envelope>
I have tried a few different ways but so far I have no success, here is my latest attempt.
#XmlRootElement(name = "soap:Envelope")
public class Envelope {
private SoapBody soapBody;
public String toString() {
return "ClassPojo [SoapBody = " + soapBody + "]";
}
public SoapBody getSoapBody() {
return soapBody;
}
#XmlElement(name = "soap:Body")
public void setSoapBody(SoapBody soapBody) {
this.soapBody = soapBody;
}
}
this converts to the following result (but it's missing the XMLNS lines):
<soap:Envelope>
<soap:Body>
<!--Other stuff-->
</soap:Body>
</soap:Envelope>
I have tried adding a namespace tag to the declaration:
#XmlRootElement(name = "soap:Envelope", namespace = "soap")
but it just made the line convert to this <ns2:soap:Envelope xmlns:ns2="soap">
Edit:
OutputStream os = connection.getOutputStream();
JAXBContext jaxbContext =
JAXBContext.newInstance(MyOtherStuffObject.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(myObject, os);
os.flush();
I have tried adding a namespace tag to the declaration:
#XmlRootElement(name = "soap:Envelope", namespace = "soap")
but it just made the line convert to this
you are in one step out of what you need...
soap namespace is http://schemas.xmlsoap.org/soap/envelope/ not soap so... what if it will be like that?
#XmlRootElement(name = "soap:Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
BTW. but do you really need to create SOAP Envelope manually? actually standard package javax.xml.soap has everything to work with SOAP where you can wrap your "other stuff" into SOAP Envelope and do not care about building it by your-self?
UPDATED:
I strongly recommend to use normal frameworks when working with SOAP web services, like Apache CXF or such, instead of manipulating SOAP on that low level.
But it can be done with standard JDK classes.
Example code:
package com.foo.tests;
import java.io.ByteArrayOutputStream;
import java.util.Calendar;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Document;
public class TestSOAPMessage {
static MessageFactory factory;
static DocumentBuilderFactory documentFactory;
static JAXBContext jaxbCtx;
static com.foo.tests.pojo.ObjectFactory myStuffFactory = new com.foo.tests.pojo.ObjectFactory();
static {
try {
factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
documentFactory = DocumentBuilderFactory.newInstance();
jaxbCtx = JAXBContext.newInstance(com.foo.tests.pojo.MyStuffPojo.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
try {
// prepare test MyStuff JAXB POJO
com.foo.tests.pojo.MyStuffPojo myStuff = myStuffFactory.createMyStuffPojo();
// populate myStuff Pojo
myStuff.setMyPropertyA("property A");
myStuff.setTimestamp(Calendar.getInstance());
myStuff.setMessageId(UUID.randomUUID().toString());
//---
// marshal JAXB Pojo to DOM Document
Document myStuffDoc = documentFactory.newDocumentBuilder().newDocument();
//*** myStuff has #XmlRootElement annotation
jaxbCtx.createMarshaller().marshal(myStuff, myStuffDoc);
//*** myStuff does not have #XmlRootElement annotation wrap it and use JAXBElement instead
// JAXBElement<com.foo.tests.pojo.MyStuffPojo myStuff> jaxbWrapper = myStuffFactory.createMyStuffPojo(myStuff);
// jaxbCtx.createMarshaller().marshal(jaxbWrapper, myStuffDoc);
//marshal JAXB Pojo to DOM Document
Document myStuffDoc = documentFactory.newDocumentBuilder().newDocument();
jaxbCtx.createMarshaller().marshal(jaxbWrapper, myStuffDoc);
//Create SOAPMessage
SOAPMessage myMessage = factory.createMessage();
//Optional if we'd like to set those properties...
myMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
myMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
// set myStuff into SOAPBody
myMessage.getSOAPBody().addDocument(myStuffDoc);
//All done. Save changes
myMessage.saveChanges();
// Just for test: print message
ByteArrayOutputStream finalBos = new ByteArrayOutputStream();
myMessage.writeTo(finalBos);
System.out.println("my Message: \r\n" + new String(finalBos.toByteArray()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
What about adding the namespace attribute in your java bean? or
JAXB also provides the #XMLSchema annotation, which we can use to generate namespace.
You can do as below.
#javax.xml.bind.annotation.XmlSchema(namespace="http://www.springframework.org/schema/beans" , elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns={ #javax.xml.bind.annotation.XmlNs(namespaceURI="http://www.w3.org/2001/XMLSchema-instance", prefix="xsi"),
#javax.xml.bind.annotation.XmlNs(namespaceURI="http://schemas.xmlsoap.org/soap/envelope/", prefix="soap")
}
)
Have also a look on this
I set up a repo which shows my problem: https://github.com/Waxolunist/stackoverflow.34392476
I try to unmarshal a simple xml document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<Export xmlns="urn:adcubum:Syrius">
<ExportInhalt/>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</Export>
</for:document>
This is the document I get from following code:
Document document = new Document();
Export export = new Export();
ExportKopf exportKopf = new ExportKopf();
exportKopf.setQuelle("lokal");
export.setExportKopf(exportKopf);
ExportInhalt exportInhalt = new ExportInhalt();
export.setExportInhalt(exportInhalt);
export.setSchemaVersion("bec811a9807a8c8da403d70b9b5e22ad");
document.setExport(export);
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(document, System.out);
Document looks as follows:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "document", namespace = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx")
public class Document {
#XmlElement(name = "Export", namespace = "urn:adcubum:Syrius")
private vo.dom.common_service.modul_bl.syrius.Export export;
}
package-info.java
#XmlSchema(
namespace = "urn:adcubum:Syrius",
xmlns = {
#XmlNs(prefix = "for", namespaceURI = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx"),
#XmlNs(prefix = "", namespaceURI = "urn:adcubum:Syrius")
},
elementFormDefault = XmlNsForm.UNQUALIFIED)
When I try to unmarshal it, I don't get the data mapped:
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InputStream is = this.getClass().getResourceAsStream("/requests/document_simple3.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlsr = factory.createXMLStreamReader(is);
Document document = unmarshaller.unmarshal(xmlsr, Document.class).getValue();
ExportKopf and ExportInhalt are returning null.
Instead following xml works. The only difference is the namespace prefix:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns3:Export xmlns:ns3="urn:adcubum:Syrius">
<ExportInhalt/>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</ns3:Export>
</for:document>
I am using eclipselink moxy.
What do I have to change, so that unmarshaling the marshaled document works.
I think it's always a good idea to see the actual XSD schema of your mapping whenever something strange like this is happening in JAXB. You could easily do that with the following code.
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
jaxbContext.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult streamResult = new StreamResult(new PrintWriter(System.err) {
#Override
public void close() {
}
});
streamResult.setSystemId(suggestedFileName);
return streamResult;
}
});
That will print the schema(s) that should reflect your JAXB model (you can use another writer to write them to a file). The XSD files are usually very revealing about these kind of issues. I think the problem in your case is the #XmlSchema mapping. You should try to use elementFormDefault = XmlNsForm.QUALIFIED instead. It's always a good idea to use QUALIFIED whenever you're working with multiple namespaces in your mapping.
EDIT: while the main problem with your JAXB mapping was the wrong and/or missing value for the elementFormDefault there were other things that had to be fixed for the code in your repo to work.
the Export element in the Document was missing a namespace declaration (from your example, the Document and Export elements are parts of different namespaces)
missing elementFormDefault = XmlNsForm.QUALIFIED from the export package
wrong namespace value for your main package #XmlSchema annotation (was urn:stackoverflow:exportnamespace instead of urn:stackoverflow:documentnamespace in which the Document element should be)
wrong import for jdk_jaxb/UnmarshallerTest.java - it was importing the model.eclipselink.Document instead of model.sun.Document
EDIT: Here's the updated code https://github.com/MojoJojo/stackoverflow.34392476
Okay, here's the working version with all moxy_jaxb test passing. Because you said you are using moxy, I left out the changes for model.sun.* packages. If you understand the concept below, you should be able to fix it easily on your own.
First, I cleaned up namespace declarations in your mode.* packages. Most of the times, the declarations and bindings inside package-info.java suffice. Declaring them over and over again on package, entities, fields will add to complexity and unwanted behavior. Check out this link for details. There is no need to re-declare them on the individual Models/Entities themselves, unless there is a strong reason to do otherwise. Next, the test xml itself was a little broken. Fixed the test xml with proper prefixes wherever necessary:
First, model.eclipselink.Document.java
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "document")
public class Document {
#XmlElement(name = "Export", namespace="urn:adcubum:Syrius")
private Export export;
public Export getExport() {
return export;
}
public void setExport(Export export) {
this.export = export;
}
}
model.eclipselink.package-info.java:
#XmlSchema(namespace = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx",
elementFormDefault = XmlNsForm.QUALIFIED)
package model.eclipselink;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Similar refactoring on model.eclipselink.export.packageinfo.java:
#XmlSchema(namespace = "urn:adcubum:Syrius",
elementFormDefault = XmlNsForm.QUALIFIED)
package model.eclipselink.export;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.Xml
And on Export.java:
package model.eclipselink.export;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlElementNillable;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Export")
#XmlType(name = "Export", propOrder = {
"exportInhalt",
"exportKopf",
"schemaVersion"
})
public class Export {
#XmlElement(name = "ExportKopf", required = true)
private ExportKopf exportKopf;
#XmlElement(name = "ExportInhalt", required = true)
private ExportInhalt exportInhalt;
#XmlElement(name = "SchemaVersion", required = true)
private String schemaVersion;
public ExportKopf getExportKopf() {
return exportKopf;
}
public void setExportKopf(ExportKopf exportKopf) {
this.exportKopf = exportKopf;
}
public ExportInhalt getExportInhalt() {
return exportInhalt;
}
public void setExportInhalt(ExportInhalt exportInhalt) {
this.exportInhalt = exportInhalt;
}
public String getSchemaVersion() {
return schemaVersion;
}
public void setSchemaVersion(String schemaVersion) {
this.schemaVersion = schemaVersion;
}
}
And the few tweaks to your xml files for prefixes.Here's document_prefix.xml
<?xml version="1.0" encoding="UTF-8"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns1:Export xmlns:ns1="urn:adcubum:Syrius">
<ns1:ExportKopf>
<ns1:Quelle>lokal</ns1:Quelle>
</ns1:ExportKopf>
<ns1:ExportInhalt/>
<ns1:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns1:SchemaVersion>
</ns1:Export>
</for:document>
document.xml:
<?xml version="1.0" encoding="UTF-8"?>
<for:document
xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx" xmlns="urn:adcubum:Syrius">
<Export>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<ExportInhalt />
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</Export>
</for:document>
and document_realnamespace.xml (Not sure what the purpose of this file is):
<?xml version="1.0" encoding="UTF-8"?>
<for:document xmlns:ns1="urn:adcubum:Syrius" xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns1:Export>
<ns1:ExportKopf>
<ns1:Quelle>lokal</ns1:Quelle>
</ns1:ExportKopf>
<ns1:ExportInhalt/>
<ns1:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns1:SchemaVersion>
</ns1:Export>
</for:document>
And you run mvn clean test:
Running moxy_jaxb.MarshallerTest
Context class: class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrend
erer/forwktbx" xmlns:ns0="urn:adcubum:Syrius">
<ns0:Export>
<ns0:ExportInhalt/>
<ns0:ExportKopf>
<ns0:Quelle>lokal</ns0:Quelle>
</ns0:ExportKopf>
<ns0:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns0:SchemaVersion>
</ns0:Export>
</document>
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
Running moxy_jaxb.UnmarshallerTest
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec
It seems there is a bug in the MOXy. The piece of code below works perfectly when fields in class Request declared as metaInfo and then content, but test fails on unmarshalling with exception when fields declared in reverse order (content first and metaInfo second).
The exception thrown is:
Going with type: APPLICATION_XML
Original request = {content=Payload = {[one, two, three]}, metaInfo=requestMetaInfo = {confirmation=false}}
Marshaled as application/xml: <?xml version="1.0" encoding="UTF-8"?><request><collection><collection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">one</collection><collection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">two</collection><collection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">three</collection></collection><metaInfo><confirmation>false</confirmation></metaInfo></request>
Local Exception Stack:
Exception [EclipseLink-32] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to set value [[one, two, three]] for instance variable [collection] of type [java.util.Collection] in the object. The specified object is not an instance of the class or interface declaring the underlying field, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: Can not set java.util.Collection field test2.TestCase2$Payload.collection to test2.TestCase2$RequestMetaInfo
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[collection]
Descriptor: XMLDescriptor(test2.TestCase2$Payload --> [DatabaseTable(collection)])
at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(DescriptorException.java:703)
at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:188)
at org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1652)
at org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping.setAttributeValueInObject(XMLCompositeCollectionMapping.java:741)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.setContainerInstance(XMLCompositeCollectionMappingNodeValue.java:265)
at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endDocument(UnmarshalRecordImpl.java:628)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endDocument(AbstractSAXParser.java:745)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:515)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:243)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:978)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:635)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:706)
at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:643)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:339)
at test2.TestCase2.main(TestCase2.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.IllegalArgumentException: Can not set java.util.Collection field test2.TestCase2$Payload.collection to test2.TestCase2$RequestMetaInfo
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
at java.lang.reflect.Field.set(Field.java:741)
at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:141)
... 24 more
Here is a test to reproduce the problem.
package test2;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
/**
* Test that fails if Request.content field declared before than Request.metaInfo, but works well if
* Request.metaInfo goes first in declaration
*
* MOXy 2.6.0
*/
public class TestCase2 {
#XmlRootElement(name = "request")
#XmlAccessorType(XmlAccessType.FIELD)
public static class Request<T> {
#XmlAnyElement(lax = true)
private T content; // NB!: Causes test failure if declared before metaInfo, if declaration order is changed, things work
#XmlElement
private RequestMetaInfo metaInfo;
public Request() {
}
public Request(T content, RequestMetaInfo metaInfo) {
this.content = content;
this.metaInfo = metaInfo;
}
#Override
public String toString() {
return "request = {" + "content=" + content + ", metaInfo=" + metaInfo +'}';
}
}
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public static class RequestMetaInfo {
#XmlElement
private Boolean confirmation = false;
#Override
public String toString() {
return "requestMetaInfo = {" + "confirmation=" + confirmation +'}';
}
}
#XmlRootElement(name = "collection")
#XmlAccessorType(XmlAccessType.FIELD)
public static class Payload<T> {
#XmlElement
private Collection collection = new ArrayList();
public Payload(){}
public Payload(Collection<T> collection){
this.collection.addAll(collection);
}
public Collection<T> getCollection() {
return collection;
}
#Override
public String toString() {
return "Payload = {" + getCollection()+"}";
}
}
// Element name holding value of primitive types
public static final String VALUE_ELEMENT = "value";
// Attribute prefix in JSON
public static final String ATTRIBUTE_PREFIX = "#";
public static void main(String... args) {
try {
for (MediaType type : new MediaType[]{MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) {
System.out.println("Going with type: " + type);
JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(
new Class[]{
Request.class,
RequestMetaInfo.class,
Payload.class
},
Collections.emptyMap());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, type);
marshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
marshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
Request original = new Request(
new Payload(Arrays.asList("one","two","three")),
new RequestMetaInfo()
);
System.out.println("Original " + original.toString());
StreamResult result = new StreamResult(new StringWriter());
marshaller.marshal(original, result);
String generated = result.getWriter().toString();
System.out.println("Marshaled as " + type.getMediaType() + ": " + generated);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, type);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
unmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
Request unmarshalled = unmarshaller.unmarshal(new StreamSource(new StringReader(generated)), Request.class).getValue();
System.out.println("Unmarshaled " + unmarshalled.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please any ideas what can be wrong?
After some debugging I found out, that bug could be probably in org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endDocument().
The collection populatedContainerValues didn't get nulled after Payload.collection was unmarshalled. Then, when moxy unmarshalls metaInfo element it tries to process it as it were Payload.collection, assigning collection to Request.metaInfo, which causes exception.
I did ugly workaround (since I can't fix it) and just changed order of fields declaration in Request object, but I believe it will be fixed one day in MOXy.
UPDATE:
I filed a bug to MOXy bugzilla: https://bugs.eclipse.org/bugs/show_bug.cgi?id=468337
I had the same problem.
My solution:
#XmlMixed
#XmlAnyElement(lax = true)
private String content;
Enjoy ;)
I have following XML on input:
<root>
<response1></response1>
</root>
or
<root>
<response2></response2>
</root>
And there is possibly a lot of response tags each of which I need to map to a single Response class because they have almost the same structure.
Is it easy to do in JAXB?
Thanks.
This could be done with the #XmlElements annotation:
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Root {
#XmlElements({
#XmlElement(name="response1", type=Response.class),
#XmlElement(name="response2", type=Response.class),
#XmlElement(name="response3", type=Response.class)
})
private Response response;
}
http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html
Well, sure. In XSD file, define a type first:
<xs:complexType name="response">
<!-- define type here -->
</xs:complexType>
Now define your elements using it:
<xs:element name="response1" type="response"/>
<xs:element name="response2" type="response"/>
<!-- and so on and so forth -->
I got it to work this way. It uses an XMLStreamReader as the source, and a StreamReaderDelegate to intercept and rewrite the element names before they reach jaxb.
The main test class:
package grimbo.test.jaxb;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
public class JaxbTest {
public static <T> T unmarshal(Class<T> clazz, InputStream inputStream) throws JAXBException, XMLStreamException,
FactoryConfigurationError {
XMLStreamReader r = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
MyXMLStreamReader my = new MyXMLStreamReader(r);
String packageName = clazz.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance(packageName);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(my);
}
public static void main(String[] args) throws Exception {
String xml1 = "<root>" + "<response1>test1</response1>" + "</root>";
String xml2 = "<root>" + "<response2>test2</response2>" + "</root>";
Object ob = unmarshal(Response.class, new ByteArrayInputStream(xml1.getBytes()));
System.out.println(ob);
ob = unmarshal(Response.class, new ByteArrayInputStream(xml2.getBytes()));
System.out.println(ob);
}
static class MyXMLStreamReader extends StreamReaderDelegate {
public MyXMLStreamReader(XMLStreamReader reader) {
super(reader);
}
public QName getName() {
QName qname = super.getName();
return qname;
}
public String getLocalName() {
String localName = super.getLocalName();
if (localName.matches("response\\d+")) {
return "response";
}
return localName;
}
}
}
The Response class is:
package grimbo.test.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "root", namespace = "")
public class Response {
String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
#Override
public String toString() {
return "Response [response=" + response + "]";
}
}
And there's a jaxb.index file in this package too, that declares the Response class:
Response
The output of the test is:
Response [response=test1]
Response [response=test2]
Is this any help?
The easiest thing to do imo would be to make the response element an unbounded list in your schema then once you have created your bindings you can iterate throught the list of response nodes.
I tried to map multiple tag to single class using JAXB with same format mention above.
Now define your elements using it:
<xs:element name="response1" type="response"/>
<xs:element name="response2" type="response"/>
<!-- and so on and so forth -->
While unmarshalling JAXB validates the XML(format) with response class format mentioned in XSD file ,but its is giving me JAXB.element class object instead of response object.
Please suugest with answer..