有什么办法可以为JAXB的集合指定defaultValue吗?

时间:2014-12-17 15:12:06

标签: java xml jaxb xsd xjc

我在Java中有这个属性

@XmlList
@XmlElement(defaultValue = "COMMENTS CASE_INSENSITIVE")
protected List<RegexFlag> regexFlags;

XJC生成的,源自此XSD:

<element name="regexFlags" type="tns:RegexFlags" 
    minOccurs="0" maxOccurs="1" default="COMMENTS CASE_INSENSITIVE"/>

<simpleType name="RegexFlags">
  <list itemType="tns:RegexFlag"/>
</simpleType>

<simpleType name="RegexFlag">
  <restriction base="string">
    <enumeration value="UNIX_LINES"/>
    <enumeration value="CASE_INSENSITIVE"/>
    <enumeration value="COMMENTS"/>
    <enumeration value="MULTILINE"/>
    <enumeration value="LITERAL"/>
    <enumeration value="DOTALL"/>
    <enumeration value="UNICODE_CASE"/>
    <enumeration value="CANON_EQ"/>
    <enumeration value="UNICODE_CHARACTER_CLASS"/>
  </restriction>
</simpleType>

不幸的是,这似乎不起作用。默认值未正确解组。我没有<regexFlags/>元素时获得的值实际上只是一个空列表。我究竟做错了什么?这对JAXB来说是否可能?

2 个答案:

答案 0 :(得分:1)

defaultValue注释上的@XmlElement属性是JAXB(JSR-222)实现应该为空元素的值交换的。当该元素映射到用@XmlList注释的属性时,它们似乎是引用和MOXy实现中的错误。

域模型

<强>根

以下是一个示例类,其中包含3个String和3个List<String>字段,所有字段都注明了@XmlElement(defaultValue="a b c")List<String>字段也标有@XmlList

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement(defaultValue="a b c")
    String singleMissingElement;

    @XmlElement(defaultValue="a b c")
    String singleEmptyElement;

    @XmlElement(defaultValue="a b c")
    String singlePopulatedElement;

    @XmlElement(defaultValue="a b c")
    @XmlList
    List<String> listMissingElement;

    @XmlElement(defaultValue="a b c")
    @XmlList
    List<String> listEmptyElement;

    @XmlElement(defaultValue="a b c")
    @XmlList
    List<String> listPopulatedElement;

}

演示代码

<强>演示

下面是一些演示代码,它解组一些XML并从对象输出结果字段。 XML元素是根据字段名称填充的(即missing表示缺少XML,empty表示空元素,populated表示带有值的元素。

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        StringReader xml = new StringReader("<root><singleEmptyElement/><singlePopulatedElement>populated</singlePopulatedElement><listEmptyElement/><listPopulatedElement>populated</listPopulatedElement></root>");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Root root = (Root) unmarshaller.unmarshal(xml);

        System.out.println(root.singleMissingElement);
        System.out.println(root.singleEmptyElement);
        System.out.println(root.singlePopulatedElement);

        System.out.println(root.listMissingElement);
        System.out.println(root.listEmptyElement);
        System.out.println(root.listPopulatedElement);
    }

}

<强>输出

意外出现的唯一值是第5个,它对应于List<String>字段的空元素。基于defaultValue我希望它是List,其中包含字符串abc

null
a b c
populated
null
[]
[populated]

为什么这是defaultValue@XmlElement的行为?

XML架构(schema.xsd)

defaultValue注释上的@XmlElement属性对应于XML架构中default声明的element属性。下面是我们在Java模型中注释的模式。

<?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="root" type="root"/>

  <xs:complexType name="root">
    <xs:sequence>
      <xs:element name="singleMissingElement" type="xs:string" default="a b c" minOccurs="0"/>
      <xs:element name="singleEmptyElement" type="xs:string" default="a b c" minOccurs="0"/>
      <xs:element name="singlePopulatedElement" type="xs:string" default="a b c" minOccurs="0"/>
      <xs:element name="listMissingElement" minOccurs="0" default="a b c">
        <xs:simpleType>
          <xs:list itemType="xs:string"/>
        </xs:simpleType>
      </xs:element>
      <xs:element name="listEmptyElement" minOccurs="0" default="a b c">
        <xs:simpleType>
          <xs:list itemType="xs:string"/>
        </xs:simpleType>
      </xs:element>
      <xs:element name="listPopulatedElement" minOccurs="0" default="a b c">
        <xs:simpleType>
          <xs:list itemType="xs:string"/>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

演示代码

下面是一些代码,它们会在启用了架构验证的情况下执行SAX解析,ContentHandler将一些信息转储到System.out

import java.io.*;
import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class ParseDemo {

    public static void main(String[] args) throws Exception {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File("src/forum27528698/schema.xsd")); 

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setSchema(schema);
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(new MyHandler());

        StringReader xml = new StringReader("<root><singleEmptyElement/><singlePopulatedElement>populated</singlePopulatedElement><listEmptyElement/><listPopulatedElement>populated</listPopulatedElement></root>");
        InputSource input = new InputSource(xml);
        xr.parse(input);
    }

    private static class MyHandler extends DefaultHandler {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            System.out.print("<" + qName + ">");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            System.out.print(new String(ch, start, length));
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.println("</" + qName + ">");
        }

    }

}

<强>输出

在输出中,我们看到默认值已应用于空元素,但不是缺少的元素。

<root>
<singleEmptyElement>a b c</singleEmptyElement>
<singlePopulatedElement>populated</singlePopulatedElement>
<listEmptyElement>a b c</listEmptyElement>
<listPopulatedElement>populated</listPopulatedElement>
</root>

答案 1 :(得分:0)

似乎存在对JAXB在原始问题中defaultValue的想法的误解(或者坦率地说,只是一个错误)。这段代码解释了它:

import static java.lang.System.out;
import static javax.xml.bind.JAXB.unmarshal;

import java.io.StringReader;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlRootElement;

public class Test {
    public static void main(String[] args) {
        out.println(unmarshal(new StringReader("<x></x>"), X.class));
        out.println(unmarshal(new StringReader("<x><flags/></x>"), X.class));
        out.println(unmarshal(new StringReader("<x><flags>X Y</flags></x>"), X.class));
    }
}

@XmlRootElement
class X {

    @XmlList
    @XmlElement(defaultValue = "A B")
    protected List<String> flags;

    @Override
    public String toString() {
        return "X [flags=" + flags + "]";
    }
}

以上的输出是

X [flags=null]
X [flags=[A, B]]
X [flags=[X, Y]]

所以,显然,仅仅存在相关元素很重要。如果不存在,则不应用defaultValue。如果它存在但值不存在,则应用defaultValue

解决方法

以下解决方法可用于强制执行这些defaultValues

public class Test {
    public static void main(String[] args) {
        X x1 = unmarshal(new StringReader("<x></x>"), X.class);
        X x2 = unmarshal(new StringReader("<x><flags/></x>"), X.class);
        X x3 = unmarshal(new StringReader("<x><flags>X Y</flags></x>"), X.class);

        out.println("First unmarshal:");
        out.println(x1);
        out.println(x2);
        out.println(x3);

        // Marshal the xml again. This will add the <flags/> element
        StringWriter s1 = new StringWriter(); JAXB.marshal(x1, s1);
        StringWriter s2 = new StringWriter(); JAXB.marshal(x2, s2);
        StringWriter s3 = new StringWriter(); JAXB.marshal(x3, s3);

        // Now we're talking!
        x1 = unmarshal(new StringReader(s1.toString()), X.class);
        x2 = unmarshal(new StringReader(s2.toString()), X.class);
        x3 = unmarshal(new StringReader(s3.toString()), X.class);

        out.println();
        out.println("Second unmarshal:");
        out.println(x1);
        out.println(x2);
        out.println(x3);
    }
}

并且注释必须从属性移动到getter,并且列表的显式延迟初始化(这就是为什么这对我来说闻起来像一个bug)。

@XmlRootElement
class X {

    protected List<String> flags;

    @XmlList
    @XmlElement(defaultValue = "A B")
    public List<String> getFlags() {
        if (flags == null)
            flags = new ArrayList<>();

        return flags;
    }

    @Override
    public String toString() {
        return "X [flags=" + flags + "]";
    }
}

看哪,输出现在是正确的&#34; ...

First unmarshal:
X [flags=null]
X [flags=[A, B]]
X [flags=[X, Y]]

Second unmarshal:
X [flags=[A, B]]
X [flags=[A, B]]
X [flags=[X, Y]]

这一切都让我想起......

System.gc();
System.gc(); // Just to be sure

- )