JAXB如何生成这样的元素< _ />我的对象?

时间:2015-06-21 16:24:43

标签: java xml jaxb

这是我的目标:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"profile", "request", "filter"})
@XmlRootElement(name = "SubmitXml")
public class SubmitXml {
    @XmlElement(name = "Profile")
    protected String profile;
    @XmlElement(name = "Request")
    protected SubmitXml.Request request;
    @XmlElement(name = "Filter")
    protected SubmitXml.Filter filter;

    public String getProfile() {
        return profile;
    }

    public void setProfile(String value) {
        this.profile = value;
    }

    public SubmitXml.Request getRequest() {
        return request;
    }

    public void setRequest(SubmitXml.Request value) {
        this.request = value;
    }

    public SubmitXml.Filter getFilter() {
        return filter;
    }

    public void setFilter(SubmitXml.Filter value) {
        this.filter = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
            "any"
    })
    public static class Filter {

        @XmlAnyElement(lax = true)
        protected Object any;

        public Object getAny() {
            return any;
        }

        public void setAny(Object value) {
            this.any = value;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
            "any"
    })
    public static class Request {

        @XmlAnyElement(lax = true)
        @XmlElementRefs({
                @XmlElementRef(name = "AirAvailability", type = AirAvailability.class)
        })
        protected Object any;

        public Object getAny() {
            return any;
        }

        public void setAny(Object value) {
            this.any = value;
        }
    }
}

我想将它编组到下面的xml中:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope>
    <soap:Body><SubmitXml>
        <Profile>DynGalileoProd_7OQ7</Profile>
        <Request>
            <AirAvailability_12 xmlns=\"\">
                <AirAvailMods>
                    <GenAvail>
                        <NumSeats>2</NumSeats>
                        <Class>Y</Class>
                        <StartDt>20151201</StartDt>
                        <StartPt>TPE</StartPt>
                        <EndPt>HKG</EndPt>
                        <StartTm><![CDATA[ ]]></StartTm>
                    </GenAvail>
                </AirAvailMods>
            </AirAvailability_12>
        </Request>
        <Filter><_ /></Filter>
    </SubmitXml>
</soap:Body>

问题是如何在元素Filter?

中生成像<_ />这样的元素

另一个问题是如何在元素<![CDATA []]>生成元素文本,例如<StartTm>?字符<>始终更改为&lt;&gt;

1 个答案:

答案 0 :(得分:0)

使用这种方法:

<T> JAXBElement<T> wrap( String ns, String tag, T o ){
    QName qtag = new QName( ns, tag );
    Class<?> clazz = o.getClass();
    @SuppressWarnings( "unchecked" )
    JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
    return jbe;
}

可以做到

Filter filter = new Filter();
String s = "";
filter.setAny( wrap( "", "_", s ) );

这将创建

 <Filter><_></_></Filter>

由于<_/>根据定义与<_></_>相同,因此您拥有所需的内容。

如果不编写自己的XML编写器,则无法完成第二个请求。但是,这样做绝对没有必要。在手动编辑XML时,使用或不使用<![CDATA []]>是一个方便的问题。如果XML编写器正确地转义<>,则没有理由选择使用CDATA。