不可支持但需要和null值

时间:2012-12-21 16:44:46

标签: jaxb

我刚和我的同事发现了一个奇怪的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

$

尊重。

1 个答案:

答案 0 :(得分:3)

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

获取更多信息

注意:我猜你是在向我提出这个问题:)。