簡體   English   中英

當值是十進制,但沒有xsi:nil =“ true”時,Jaxb marshelling預期元素中的xsi:nil =“ true”

[英]Jaxb marshelling expected xsi:nil=“true” in the element when the value is decimal which will be null, but xsi:nil=“true” is not coming

下面的代碼正在生成下面給出的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);
    }
}

xsi:nil="true"將表示Market.Detail對象,其shipping屬性為null 這里不是這種情況-您有一個非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表示,該JAXBElementisNil()返回true ,而getValue()Shipping的非空實例,其getAvailable()返回false


在對問題進行最新編輯之后-現在,您已經具有創建JAXBElement<Shipping>實例的基礎結構,您只需要根據需要設置nil屬性即可:

    Market.Detail.Shipping mds = new  Market.Detail.Shipping();
    mds.setAvailable(false);
    JAXBElement<Market.Detail.Shipping> shipping = new ObjectFactory().createShipping(mds);
    shipping.setNil(true); // mark the element as nil

    md.setShipping(shipping);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM