在jaxb中手动为嵌套命名空间创建注释类

时间:2015-10-22 15:25:13

标签: java xml namespaces jaxb

我想要有JAXB注释的类,这些类将被编组/解组到不同的XML名称空间。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://test.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <v1:UpdateMemberRequest>
         <v1:memberID>568</v1:memberID>
         <v1:member>
            <v1:Address>USA</v1:Address>
         </v1:member>
      </v1:UpdateMemberRequest>
   </soapenv:Body>
</soapenv:Envelope>

课程怎么样?

1 个答案:

答案 0 :(得分:0)

在所需的Java类之下。根据标准惯例添加getter和setter。在package com.test;

// MemberType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MemberType", propOrder = {
    "address"
})
public class MemberType {
    @XmlElement(name = "Address", required = true)
    protected String address;
}

// UpdateMemberRequestType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UpdateMemberRequestType", propOrder = {
    "memberID",
    "member"
})
public class UpdateMemberRequestType {
    @XmlElement(required = true)
    protected String memberID;
    @XmlElement(required = true)
    protected MemberType member;
}
// ObjectFactory.java
@XmlRegistry
public class ObjectFactory {

    private final static QName _UpdateMemberRequest_QNAME =
    new QName("http://test.com/", "UpdateMemberRequest");
    public ObjectFactory() {
    }
    public UpdateMemberRequestType createUpdateMemberRequestType() {
        return new UpdateMemberRequestType();
    }
    public MemberType createMemberType() {
        return new MemberType();
    }
    @XmlElementDecl(namespace = "http://test.com/",
                    name = "UpdateMemberRequest")
    public JAXBElement
    createUpdateMemberRequest(UpdateMemberRequestType value) {
        return new JAXBElement(_UpdateMemberRequest_QNAME,
           UpdateMemberRequestType.class, null, value);
    }
}

另一个package org.xmlsoap.schemas.soap.envelope;

// BodyType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BodyType", propOrder = {
    "updateMemberRequest"
})
public class BodyType {
    @XmlElement(name = "UpdateMemberRequest", namespace = "http://test.com/", required = true)
    protected UpdateMemberRequestType updateMemberRequest;
}

// EnvelopeType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EnvelopeType", propOrder = {
    "header",
    "body"
})
public class EnvelopeType {
    @XmlElement(name = "Header", required = true)
    protected String header;
    @XmlElement(name = "Body", required = true)
    protected BodyType body;
}
// ObjectFactory.jav
@XmlRegistry
public class ObjectFactory {
    private final static QName _Envelope_QNAME =
        new QName("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
    public ObjectFactory() {
    }
    public EnvelopeType createEnvelopeType() {
        return new EnvelopeType();
    }
    public BodyType createBodyType() {
        return new BodyType();
    }
    @XmlElementDecl(namespace = "http://schemas.xmlsoap.org/soap/envelope/",
                    name = "Envelope")
    public JAXBElement createEnvelope(EnvelopeType value) {
        return new JAXBElement(_Envelope_QNAME,
            EnvelopeType.class, null, value);
    }
}