我希望超类的字段在下面的场景中被解组,但事实并非如此。该怎么做才能让它发挥作用?
输入:
<?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