我希望在下面的场景中对父类(super class)的字段进行解码,但事实并非如此。怎么做才能让它发挥作用?

输入:

<?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>

类(class):
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

最佳答案

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

选项 #1 - 在 SuperClass 上映射 SubClass 属性

如果要将父类(super class)属性映射为子类的一部分,则需要在 java-type 元素上标记父类 transient 。

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

关于inheritance - EclipseLink MOXy : Superclass fields are not set when unmarshaling,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17981925/

10-15 10:59