Let me know the best way to serialize my Java object Download. This is a class generated from a java wsimport tool from a WSDL.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Download", propOrder = {
"Response",
"VendorInformation",
"DownloadItem",
"DownloadCommentItem",
"DownloadIntercomItem"
})
public class Download
{
@XmlElement(name = "Response")
protected ResponseMessageManagementType Response;
@XmlElement(name = "VendorInformation")
protected DownloadVendorInformation VendorInformation;
@XmlElement(name = "DownloadItem")
protected List<DownloadDownloadItem> DownloadItem;
@XmlElement(name = "DownloadCommentItem")
protected ArrayOfDownloadDldComment DownloadCommentItem;
@XmlElement(name = "DownloadIntercomItem")
protected ArrayOfDownloadDldIntercom DownloadIntercomItem;
.........................
}
The java classes generated from the tool do not have any serlization implementation. And I want to serialize the Download class following this kind of format:
<?xml version="1.0" encoding="utf-8"?>
<Download xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www开发者_运维百科.w3.org/2001/XMLSchema-instance"
xmlns="HTTP://xyz.abc.Com//Vendor/DownloadWSE.xsd">
<Response>
.....
</Response>
<VendorInformation>
...............
</VendorInformation>
<DownloadItem>
<DownloadDownloadItem>
.......
</DownloadDownloadItem>
<DownloadDownloadItem>
.......
</DownloadDownloadItem>
<DownloadDownloadItem>
.......
</DownloadDownloadItem>
</DownloadItem>
<DownloadCommentItem>
........
</DownloadCommentItem>
<DownloadIntercomItem>
........
</DownloadIntercomItem>
</Download>
You can see the mapping between XmlElementName and the content of the XML string. I am at loss on how to do this.
Thanks
This is JAXB. You would need:
JAXBContext ctx = JAXBConetxt.newInstance(Download.class);
Marshaller m = ctx.createMarshaller();
m.marshal(downloadObject, out);
where out
can be lots of things, including OutputStream
, Writer
and File
. If you want to get it as a String
, use a StringWriter
This is JAXB, and to get your example working you need to supply root element and namespace information:
Root Element
When you marshal an object with JAXB it requires information about the root element. One way to do this is to annotate your Download
class with @XmlRootElement
@XmlRootElement(name="Download")
public class Download
If you cannot do that you will need to wrap your instance of Download
in a JAXBElement
:
Download download = new Download();
QName qname = new QName("HTTP://xyz.abc.Com//Vendor/DownloadWSE.xsd";
JAXBElement<Download> jaxbElement = new JAXBElement(qname, "Download"), Download.class, download);
Namespace Qualification
Also to get the namespace qualification you are after you can use the package level @XmlSchema
annotation:
@XmlSchema(
namespace="HTTP://xyz.abc.Com//Vendor/DownloadWSE.xsd",
elementFormDefault=XmlNsForm.QUALIFIED)
package your.model.package.containing.download;
import javax.xml.bind.annotation.*;
Demo
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Download.class);
Download download = new Download();
QName qname = new QName("HTTP://xyz.abc.Com//Vendor/DownloadWSE.xsd";
JAXBElement<Download> jaxbElement = new JAXBElement(qname, "Download"), Download.class, download);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(jaxbElement, System.out);
}
}
精彩评论