在下面的场景中,我会喜欢java-type name="SubClass"
应用的绑定来设置 SuperClass 上的文本字段。然而事实并非如此。覆盖 bindingsA.xml 有问题吗?根据覆盖规则文档:
如果多个文件中出现相同的 java-type,则在后面的文件中设置的任何值都将覆盖前面文件中的值
我需要做什么才能使其工作?
输入:
<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>
绑定 A:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="test">
<java-types>
<java-type name="SuperClass">
<xml-root-element name="a"/>
<java-attributes>
<xml-element java-attribute="text" xml-path="@text" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
绑定 B:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="test">
<java-types>
<java-type name="SuperClass" xml-transient="true"></java-type>
<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);
List<String> bindings = new LinkedList<String>();
bindings.add("bindingsA.xml");
bindings.add("bindingsB.xml");
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
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 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text