【问题标题】:JAXB : trouble in unmarshelling field of type List<String> with @XmlAttributeJAXB:使用 @XmlAttribute 解组 List<String> 类型的字段时遇到问题
【发布时间】:2014-02-05 07:22:37
【问题描述】:

我正在使用 JAXB 编组和解组对象。我有一个Class MyBean,其中包含字符串列表作为字段并用@XmlAttribute 注释。

@XmlRootElement
public class MyBean {
    public MyBean() {
        super();

    }
    private List<String> actualValue;

    @XmlAttribute
    public List<String> getActualValue() {
        if (actualValue == null) {
            actualValue = new ArrayList<String>();
        }
        return actualValue;
    }
    public void setActualValue(List<String> values) {
        this.actualValue = values;
    }
}

以下是用于编组和取消编组 MyBean 的 Test 类。

public class Test {

public static void main(String[] args) {

    try {
        // create object ....
        MyBean myBean = new MyBean();
        List<String> values = new ArrayList<String>();
        values.add("Sanjv Singh Baghel");
        myBean.setActualValue(values);



        // Marsheling
        File file = new File("D://Temp//hello2.xml");
        JAXBContext jaxbContext2 = JAXBContext.newInstance(MyBean.class);
        Marshaller jaxbMarshaller2 = jaxbContext2.createMarshaller();
        jaxbMarshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller2.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller2.marshal(myBean, file);

        // Un-Marsheling
        JAXBContext jaxbContext = JAXBContext.newInstance(MyBean.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        // printing actual value
        MyBean myBean2 = (MyBean) jaxbUnmarshaller.unmarshal(file);
        List<String> actualValue = myBean2.getActualValue();
        System.out.println(actualValue);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

}

输出:

Before actualValue : ["sanjv singh baghel"]
After  actualValue : ["sanjv, singh, baghel"]

奇怪的是,我在这里遇到的是原来的actualValue 只有一个带空格的字符串,在解组后,该单个字符串被转换为字符串列表(除以空格)。

我想知道为什么会这样/有什么问题。

为此,我找到了两种可能的解决方案:

  • @XmlAttribute 替换为@XmlElement
  • List&lt;String&gt; actualValue; & String 需要映射到 schema 简单类型

【问题讨论】:

    标签: java jaxb marshalling unmarshalling


    【解决方案1】:

    XML Schema 具有数据类型列表的概念。

    <xs:attribute name="actualValue">
         <xs:simpleValue>
             <xs:list itemType="xs:string"/>
         <xs:simpleValue>
    </xs:attribute>
    

    这在 XML 中表示为空格分隔的值。

    <foo actualValue="one two three"/>
    

    这就是您通过将@XmlAttribute 注释放在List&lt;String&gt; 类型的属性上来要求JAXB 做的事情。请参阅 @XmlAttribute 的 javadoc 中的“示例 3”。

    如果您将@XmlAttribute 替换为@XmlElement,您将获得以下表示:

    <foo>
        <actualValue>one</actualValue>
        <actualValue>two</actualValue>
        <actualValue>three</actualValue>
    </foo>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2021-09-27
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多