序列化 Java 对象

发布于 2024-11-08 21:33:54 字数 1812 浏览 0 评论 0原文

让我知道序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必在意 2024-11-15 21:33:54

这是 JAXB。您需要:

JAXBContext ctx = JAXBConetxt.newInstance(Download.class);
Marshaller m = ctx.createMarshaller();
m.marshal(downloadObject, out);

其中 out 可以是很多东西,包括 OutputStreamWriterFile。如果您想将其作为 String 获取,请使用 StringWriter

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

绝不放开 2024-11-15 21:33:54

这是 JAXB,要使示例正常工作,您需要提供根元素和命名空间信息:

根元素

当您使用 JAXB 封送对象时,它需要有关根元素的信息。一种方法是使用 @XmlRootElement 注释您的 Download 类。

@XmlRootElement(name="Download")
public class Download 

如果您无法做到这一点,则需要将 Download 实例包装在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);

命名空间限定

另外,要获得命名空间限定,您可以使用包级别 @XmlSchema 注释:

@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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文