EclipseLink MOXy:解组时不设置超类字段

时间:2013-07-31 22:15:07

标签: inheritance jaxb eclipselink moxy

我希望在下面的场景中对超类的字段进行解组,但它们不是。如何使其发挥作用?

输入:

<?xml version="1.0" encoding="UTF-8"?>
<a>my text</a>

结合:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

<java-types>        
  <java-type name="SubClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="text()" />
    </java-attributes>
  </java-type>
</java-types>

</xml-bindings>

类:

public class SuperClass {

 private String text;

 public String getText() {
    return text;
 }

 public void setText(String text) {
    this.text = text;
 }
}

public class SubClass extends SuperClass { }

演示:

Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(superClass.getText());   

输出:

[EL Warning]: 2013-07-31 15:09:16.602--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
null

2 个答案:

答案 0 :(得分:2)

需要在它所属的类上进行映射。

选项#1 - 在SuperClass

上映射SubClass属性

如果要将超类属性映射为子级的一部分,则需要在java-type元素上标记父类的瞬态。

<强> bindings.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17981925">
    <java-types>
        <java-type name="SuperClass" xml-transient="true"/>
        <java-type name="SubClass">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="text" xml-path="text()" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

选项#2 - 在SuperClass

上映射SuperClass属性

或者,您可以在text上映射SuperClass属性,此映射随后由SubClass继承。

<强> bindings.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17981925">
    <java-types>
        <java-type name="SuperClass">
            <java-attributes>
                <xml-element java-attribute="text" xml-path="text()" />
            </java-attributes>
        </java-type>
        <java-type name="SubClass">
            <xml-root-element/>
        </java-type>
    </java-types>
</xml-bindings>

演示代码

可以运行以下演示代码来证明这两个选项都有效:

<强> input.xml中

<subClass>Hello World</subClass>

<强>演示

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
        jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
        System.out.println(superClass.getText());
    }

}

<强>输出

Hello World

答案 1 :(得分:0)

SuperClass的字段也可能 null - 因为 SuperClass 没有 Setter 领域, 因为Unmarshaller使用不带​​参数的Default构造函数和 Setters 来初始化他的字段。或者SuperClass没有@ XmlRootElement 和@ XmlElement 注释