序列化 Java 对象
让我知道序列化 Java 对象下载的最佳方法。这是通过 WSDL 的 java wsimport 工具生成的类。
@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 类没有任何序列化实现。 我想按照这种格式序列化 Download 类:
<?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 字符串内容之间的映射。 我不知道如何做到这一点。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 JAXB。您需要:
其中
out
可以是很多东西,包括OutputStream
、Writer
和File
。如果您想将其作为String
获取,请使用StringWriter
This is JAXB. You would need:
where
out
can be lots of things, includingOutputStream
,Writer
andFile
. If you want to get it as aString
, use aStringWriter
这是 JAXB,要使示例正常工作,您需要提供根元素和命名空间信息:
根元素
当您使用 JAXB 封送对象时,它需要有关根元素的信息。一种方法是使用
@XmlRootElement
注释您的Download
类。如果您无法做到这一点,则需要将
Download
实例包装在aJAXBElement
:命名空间限定
另外,要获得命名空间限定,您可以使用包级别
@XmlSchema
注释:Demo< /强>
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
If you cannot do that you will need to wrap your instance of
Download
in aJAXBElement
:Namespace Qualification
Also to get the namespace qualification you are after you can use the package level
@XmlSchema
annotation:Demo