Reputation: 26874
I currently have to store a Java object of a class that is not implementing Serializable
but is surely SOAP-serialized because was generated from a WSDL and is the input of a web service.
Standard Java serialization, in fact, throws exception. I have no control over the classes, and the guys who created the classes are not willing to mark them Serializable
by hand, because proxies were automatically generated from the WSDL (wsdl2java
? They haven't told me...).
So I need to transform that object into something else by not possibly cycling over each field.
The .class files show the XML serialization attributes, so I suppose they are definitely XML-serializable.
How to perform manual XML serialization of a Java object? I can do that in C# but not in Java.
Thank you
Upvotes: 0
Views: 3134
Reputation: 2480
That is one way of doing things, another is for the....people controlling the java client to be a little more reasonable. Assuming that Jax-ws was used to create the client via the wsimport utility they can specify a simple little binding file, specified in the -b option that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
elementFormDefault="qualified" attributeFormDefault="unqualified"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<xjc:serializable />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
</xs:schema>
It will automagically put "serializable" on all the generated classes. I've also got this working in Maven via the jaxws-maven-plugin. I'm sure other client generators have similar options as well.
HTH
Upvotes: 1
Reputation: 76709
If you are willing to (or someone else is willing to) put the effort into annotating the classes using JAXB annotations, then JAXB would be the straightforward choice to serialize to, and de-serialize from XML. If this is already done (your question indicated that XML serialization attributes are available in the class files), then you need to use the JAXBContext and related API classes (Marshaller and Unmarshaller) to marshal and unmarshal the objects.
If you are put off with all that effort, you can consider Castor. I should warn you that if you do not have a mapping, then you may not be satisfied with the output. But, this is the quickest and easiest way to serialize to XML, if you can import the dependency into your project*.
I would also provide obligatory links to JiBX, XMLBeans and XStream. All of them can perform marshalling to and unmarshalling from XML. Some of these would not require any mapping just like Castor.
* Having made an attempt to writing a XML serializer in the past, I would advise against writing your own code that attempts to do the work that Castor or a similar marshaller/unmarshaller does. It involves heavy amount of reflection in the absence of a mapping file.
Upvotes: 1
Reputation: 346240
If your objects follow the Java Beans spec and you don't care how exactly they're represented as XML, XMLEncoder
from the Java standard API can do the job.
If your objects are not "pure" JavaBeans or you need more control over the generated XML, XStream is a popular XML serialization library.
Upvotes: 0
Reputation: 3036
You can do
private byte[] encode(YourObject obj)
{
byte[] bytes = null;
try
{
YourObject vsNew = new YourObject(obj)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream out = new GZIPOutputStream(baos);
XMLEncoder encoder = new XMLEncoder(out);
encoder.writeObject(vsNew);
encoder.close();
bytes = baos.toByteArray();
}
catch (Exception e)
{
_log.error("Exception caught while encoding/zipping ", e);
}
return bytes;
}
/*
* Decode the report definition blob back to the
* ScheduledReport object.
*/
private YourObject decode(byte[] bytes)
{
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
YourObject vSNew = null;
try
{
GZIPInputStream in = new GZIPInputStream(bais);
XMLDecoder decoder = new XMLDecoder(in);
vSNew = (YourObject)decoder.readObject();
decoder.close();
}
catch (Exception e)
{
_log.error("IOException caught while decoding/unzipping ", e);
}
return vSNew ;
}
Upvotes: 1