- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有以下 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 的两个有效表示是:
minOccurs="0"
。这是默认行为。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/
我正在尝试执行 JavaPairRDD 和 JavaPairRDD 的 leftOuterJoin> 并且函数签名返回类型是 JavaPairRDD>>> 这里可选的是 com.google.comm
我正在尝试按元素的频率对元素进行排序 import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt
这个问题已经有答案了: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答) 已
编辑:问题已解决:请参阅 Karim SNOUSSI 的答案和我在下面的评论。 这是我在堆栈溢出时遇到的第一个问题,所以我可能不会一开始就把所有事情都做对。对此感到抱歉。此外,我对 Java 和一般
#include #include using namespace std; class Integer { public: int i; Integer (int ll
我不明白: ArrayList list = new ArrayList(); Collection list1 = new ArrayList(); 类 ArrayList扩展实现接口(interf
我编写了:。它成功了。我不知道为什么?
我编写了:。它成功了。我不知道为什么
我编写了:。它成功了。我不知道为什么?
Collectors.counting()返回 long此方法中每个键的值: private static Map countDuplicates(HashSet cards) { retur
我正在尝试通过搜索旧元素并将其替换为新元素来更新节点的元素。但是有一个我不明白的错误。是什么导致我的代码出现该错误,我该如何解决?错误; The method update(Integer, Inte
我有一个称为 client 的表,其中有一列称为created_time ,所以实际上我想绘制一个 map ,以便我可以知道在哪一年和哪一个月添加了多少客户?现在的要求是假设在 2018 年 11 月
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 8 年前。 我对 ArrayList Collecti
我意识到下面的代码是正确的 Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE) 这是因为当我们取反-21474
我有以下类 AccountWebappGridRow,它扩展了 AccountGridRow: public class AccountWebappGridRow extends AccountGri
我正在学习 Haskell 并看到了函数组合。 尝试复合 map和 foldl mapd = (map.foldl) 比 test = (mapd (\x y -> x + y ) [1,2,3,4]
我有两个相同大小的数组和两个方法。 public class Client { private static int[] ints; private static final int
我喜欢 Java 8 中的 Streams 概念。现在我想借助 Java Streams 将 Java 中的 Map 转换为排序列表。我只想显示列表而不将其存储在任何地方。我希望在结果列表中有这个输出
我有一个数据库表,其中包含电视节目类型列表和关联的 ARGB 颜色值,用于在显示电视指南时突出显示 Android ListView 中的电视节目。流派表看起来像这样... id genre
我有一个 Integer 类,它应该模拟一个整数 mod n。因此,它具有如下构造函数: Integer::Integer(int x) : m(x), n(0) { } Integer::I
我是一名优秀的程序员,十分优秀!