Jaxb marshelling预期xsi:nil =" true"在元素中,当值为十进制时将为null,但是xsi:nil =" true"不来了

时间:2014-05-09 12:01:58

标签: java xml jaxb

以下代码生成下面给出的xml而不是xsi:nil=true,为什么它没有生成该属性?

从以下演示代码中输出XML:

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <Market>
      <detail>
          <shipping available="false"/>
       </detail>
   </Market>

Jaxb类用于从marshelling生成xml输出,我还添加了@XmlElement(nillable = true)注释。它没有在xml中输出xsi:nil=true

package com.jverstry.annotations.generics;

import java.math.BigDecimal;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "detail"
})
@XmlRootElement(name = "Market")
public class Market {

    @XmlElement(required = false)
    protected Detail detail;

    public Detail getDetail() {
        return detail;
    }

    public void setDetail(Detail detail) {
        this.detail = detail;
    }

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

        @XmlElementRef(name="shipping")
        protected JAXBElement<Shipping> shipping;

        public JAXBElement<Shipping> getShipping() {
            return shipping;
        }

        public void setShipping(JAXBElement<Shipping> value) {
            this.shipping = value;
        }


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

            @XmlValue
            protected BigDecimal value;

            @XmlAttribute(name = "available")
            protected Boolean available;

            public BigDecimal getValue() {
                return value;
            }

            public void setValue(BigDecimal value) {
                this.value = value;
            }

            public Boolean getAvailable() {
                return available;
            }

            public void setAvailable(Boolean value) {
                this.available = value;
            }
        }
    }
}

ObjectFactory类:

package com.jverstry.annotations.generics;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name = "shipping")
    public JAXBElement<Market.Detail.Shipping> createShipping(Market.Detail.Shipping value) {
        return new JAXBElement<Market.Detail.Shipping>(new QName("shipping"), Market.Detail.Shipping.class, value);
    }
}

运行jaxb类以获取xml:

package com.jverstry.annotations.generics;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Market.class,ObjectFactory.class);

        Market market = new Market();  
        Market.Detail md = new Market.Detail();

        Market.Detail.Shipping mds = new  Market.Detail.Shipping();
        mds.setAvailable(false);
        JAXBElement<Market.Detail.Shipping> shipping = new ObjectFactory().createShipping(mds);

        md.setShipping(shipping);
        market.setDetail(md);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(market, System.out);
    }
}

1 个答案:

答案 0 :(得分:1)

xsi:nil="true"的存在将表示Market.Detail属性为shipping的{​​{1}}对象。这不是这种情况 - 你有一个非空的null对象,其值为空。要将Market.Detail.Shipping与其他属性结合使用,您必须将xsi:nil属性声明为shipping,而不仅仅是JAXBElement<Shipping>

Shipping

现在 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "shipping"}) public static class Detail { @XmlElement(nillable = true) protected JAXBElement<Market.Detail.Shipping> shipping; public JAXBElement<Market.Detail.Shipping> getShipping() { return shipping; } public void setShipping(JAXBElement<Market.Detail.Shipping> value) { this.shipping= value; } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "value" }) public static class Shipping { // as before } } 将由<shipping available="false" xsi:nil="true"/>表示,JAXBElement返回isNil()且其truegetValue()的非空实例} Shipping返回getAvailable()


根据您对问题的最新修改 - 现在您拥有了创建false个实例的基础架构,您只需根据需要设置JAXBElement<Shipping>属性:

nil