xml序列化Java对象
让我知道序列化Java对象下载的最佳方法。这是一个由java wsimport工具从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;
.........................
}
该工具生成的java类没有任何serlization实现。 我想按照这种格式序列化下载类:
<?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>
您可以看到XmlElementName和XML字符串内容之间的映射。 我不知道该怎么做
谢谢
# 1 楼答案
这是JAXB,为了让示例正常运行,您需要提供根元素和名称空间信息:
根元素
使用JAXB封送对象时,需要有关根元素的信息。一种方法是用
@XmlRootElement
注释Download
类如果不能这样做,则需要将
Download
的实例包装在JAXBElement
中:名称空间限定
另外,要获得名称空间限定,可以使用包级别
@XmlSchema
注释:演示