gpt4 book ai didi

jaxb - 不可为 nillable 但为必需且为 null 值

转载 作者:行者123 更新时间:2023-12-01 15:55:25 26 4
gpt4 key购买 nike

我刚和我的同事发现了一个关于 JAXB 的奇怪案例。

我用 @XmlElement(required = true) 声明了一个字段,默认为 nillable = false

但我无意中试图编码一个值为 null 的实例。

而且我看到该元素只是被省略了。正常吗?这只是程序员的错吗?

import java.io.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class Doughan {

public static void main(final String[] args) throws JAXBException, IOException {

final JAXBContext context =
JAXBContext.newInstance(Doughan.class);

final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

System.out.println("\n------------------------------------- not valid, is it?");
marshaller.marshal(new Doughan(), System.out);

System.out.println("\n------------------------------------------------- valid");
marshaller.marshal(new Doughan("Blaise"), System.out);

System.out.println("\n------------------------------------------------ schema");
context.generateSchema(new SchemaOutputResolver(){
@Override public Result createOutput(final String namespaceUri,
final String suggestedFileName)
throws IOException {
return new StreamResult(System.out) {
@Override public String getSystemId() {
return "Do I really have to do this?";
}
};
}
});
}

public Doughan() {
this(null);
}

public Doughan(final String name) {
super();
this.name = name;
}

@XmlElement(required = true) // default; nillable = false
private String name;

@XmlElement(nillable = true, required = true)
private String nill = null;
}

这是我得到的。

------------------------------------- not valid, is it?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>

-------------------------------------- valid, isn't it?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>

------------------------------------------------ schema
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doughan" type="doughan"/>
<xs:complexType name="doughan">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="nill" type="xs:string" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

xmllint

$ head -n100 doughan.xsd doughan1.xml doughan2.xml
==> doughan.xsd <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doughan" type="doughan"/>
<xs:complexType name="doughan">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="nill" type="xs:string" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
==> doughan1.xml <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
==> doughan2.xml <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>

$ xmllint --schema doughan.xsd doughan1.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
Element 'nill': This element is not expected. Expected is ( name ).
doughan1.xml fails to validate

$ xmllint --schema doughan.xsd doughan2.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
doughan2.xml validates

$

敬上。

最佳答案

是的,如果元素未使用 @XmlElement(nillable=true) 注释,则 XML 文档中将省略空值。

了解更多信息

注意:我猜你是在用这个问题针对我 :)。

关于jaxb - 不可为 nillable 但为必需且为 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993975/

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