gpt4 book ai didi

java - Jaxb 编码预期为 xsi :nil ="true" in the element when the value is decimal which will be null,,但 xsi:nil ="true"未出现

转载 作者:行者123 更新时间:2023-12-01 13:00:19 28 4
gpt4 key购买 nike

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

来自以下演示代码的 XML:

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

Jaxb 类用于从编码生成 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.Detailshipping 的对象属性是 null 。这里的情况并非如此 - 你有一个非空 Market.Detail.Shipping具有空值的对象。为了让xsi:nil与其他属性结合使用时,您必须声明 shipping属性(property)作为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()返回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);

关于java - Jaxb 编码预期为 xsi :nil ="true" in the element when the value is decimal which will be null,,但 xsi:nil ="true"未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564157/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com