我希望在下面的场景中对超类的字段进行解组,但它们不是。如何使其发挥作用?
输入:
<?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
答案 0 :(得分:2)
需要在它所属的类上进行映射。
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>
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 注释