gpt4 book ai didi

java - 从 JAXB 转换 null java.lang.Integer 产生 0,而不是 null

转载 作者:数据小太阳 更新时间:2023-10-29 02:56:50 29 4
gpt4 key购买 nike

我有以下 XML:

<person>
<id/>
<Identifier/>
<Surname>TEST</Surname>
<Forename1>TEST</Forename1>
<Forename2/>
<Title>MR</Title>
<Gender>M</Gender>
</person>

通过序列化以下类产生:

package anonymised.packagename;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "", propOrder = {
"Id",
"Identifier",
"Surname",
"Forename1",
"Forename2",
"Title",
"Sex"
})

@XmlRootElement(name = "person")
public class person{

@XmlElement(name = "id", nillable=true)
protected Integer Id;
public Integer getId(){
return Id;
}

public void setId(Integer value){
this.Id = value;
}

@XmlElement(name = "Identifier", nillable=true)
protected String Identifier;
public String getIdentifier(){
return Identifier;
}

public void setIdentifier(String value){
this.Identifier = value;
}

@XmlElement(name = "Surname", nillable=true)
protected String Surname;
public String getSurname(){
return Surname;
}

public void setSurname(String value){
this.Surname = value;
}

@XmlElement(name = "Forename1", nillable=true)
protected String Forename1;
public String getForename1(){
return Forename1;
}

public void setForename1(String value){
this.Forename1 = value;
}

@XmlElement(name = "Forename2", nillable=true)
protected String Forename2;
public String getForename2(){
return Forename2;
}

public void setForename2(String value){
this.Forename2 = value;
}

@XmlElement(name = "Title", nillable=true)
protected String Title;
public String getTitle(){
return Title;
}

public void setTitle(String value){
this.Title = value;
}

@XmlElement(name = "Gender", nillable=true)
protected String Gender;
public String getGender(){
return Gender;
}

public void setGender(String value){
this.Gender = value;
}
}

我使用对象上的“set”方法和 JAXB 序列化来生成上面的 XML,选择 java.lang.Integer 对象正是因为它们可以为 null。

然后我使用以下代码将 XML(由 xmlString 表示)反序列化回对象:

DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
String className = "anonymised.packagename." + doc.getDocumentElement().getNodeName();
Class<?> c = Class.forName(className);
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller um = context.createUnmarshaller();
Object o = c.cast(um.unmarshal(new StringReader(xmlString)));

JAXBContext jbc = JAXBContext.newInstance(c);
Marshaller jm = jbc.createMarshaller();

jm.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jm.marshal(o, new File("C:\\person.xml"));

生成以下 XML 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<id>0</id>
<Identifier/>
<Surname>TEST</Surname>
<Forename1>TEST</Forename1>
<Forename2></Forename2>
<Title>MR</Title>
<Gender>M</Gender>
</person>

原为 null 的 java.lang.Integer 现在为 0。这怎么可能?从断点来看,从 null 到 0 的变化发生在转换过程中,但我不明白为什么会这样。在类型转换过程中是否有某种转换为简单的 int ?有没有办法避免这种现象?我认为在 XML 元素声明中添加“nillable”会有所帮助,但事实并非如此。

最佳答案

JAXB 不会将任何空元素视为 null 的有效表示。如果您反省 String 类型的 indentifier 属性,您会在解码空元素后看到它是 ""

null 的两个有效表示是:

  1. 缺少元素,对应于 XML 架构中的 minOccurs="0"。这是默认行为。
  2. xsi:nil="true" 在元素上。这对应于 XML 架构中的 nillable="true"。如果您使用 @XmlElement(nillable=true) 注释您的属性,您将获得此行为。

更新

Thanks for your answer. On point 2, I have annotated my property with @XmlElement(nillable = true) in the class above, but I haven't got the behaviour you describe in my XML. Does your answer mean that, in the production of any XML that should be null, the attribute xsi:nil needs to explicitly be set to true?

当您使用 @XmlElement(nillable=true) 时,期望对应的 XML 元素的 xsi:nil 属性设置为 true 喜欢以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<id xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</person>

I'm happy with the idea that the Identifier element is an empty String, not null, but the id element, because it's an Integer should be null, not 0, right?

您绝对可以提出这样的论点。使用 JAXB RI,如果您将 Integer 属性更改为 Long,您将看到您正在寻找的行为。

关于java - 从 JAXB 转换 null java.lang.Integer 产生 0,而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115364/

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