有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将对象转换为JAXBElement<Object>

我正在尝试使用一个webservice,我需要使用元素类型“JAXBElement”来设置请求的参数。我有这个特定的对象,但我不知道如何将我的对象转换为JAXBElement。我搜索了一些相关的主题,但没有找到任何明确的答案。我需要做什么来转换这个对象


共 (1) 个答案

  1. # 1 楼答案

    XMLGenerator。类包含以下代码

    public void generateXML() {
        try {
            List<JAXBClass> jaxbClassList= this.jaxbObjectList;
            outputElement.setJAXBObject(jaxbClassList);
            marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "xsd location of the file used to create this xml");
            marshaller.marshal(outputElement,new File("Example.xml"););
    
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    
    }
    

    JAXBClass。课堂

     @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "JAXBClass", propOrder = {
        "referenceDate",
        "roles",
        "identifiers",
        "name",
        "address"
       })
    public static class JAXBClass{
    
    
        @XmlElement(name = "ReferenceDate", required = true)
        protected String referenceDate;
    
    
        @XmlElement(name = "Roles", required = true)
        protected RolesType roles;
        @XmlElement(name = "Identifiers", required = true)
        protected IdentifierType identifiers;
    
    
        @XmlElement(name = "Name", required = true)
        protected String name;
    
        @XmlElement(name = "Address", required = true)
        protected CounterPartyList.CounterPartyTO.Address address;
    
    //setter and getters of the above variables
    }
    

    用JAXBClass中的变量映射输入类,就可以用XML编写它了
    在Eclipse中,您不必编写上述类,只需右键单击xsd文件并单击generate->;JAXB类并且您拥有所需的一切

    我希望这有帮助