Moxy不能解组超出supperclass

时间:2016-06-17 02:44:15

标签: java xml jaxb moxy

当使用Moxy将样本xml解组为child时,它始终无法获取名称。它总是为空。

示例xml

<?xml version="1.0" encoding="UTF-8"?>
<child>
    <name value="test"/>
</child>

示例类

public class Parent {

    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

@XmlRootElement
public class Child extends Parent {

    @Override
    @XmlPath("name/@value")
    public String getName() {
        return super.getName() == null ? "" : super.getName();
    }

    @Override
    public void setName(String name) {
        super.setName(name);
    }
}


JAXBContext jc2 = JAXBContext.newInstance(Child.class);
Unmarshaller unmarshaller = jc2.createUnmarshaller();
Child child = (Child) unmarshaller.unmarshal(new File("d:\\sample.xml"));

如果我无法对Parent类进行任何更改,我怎样才能获得此值。

谢谢,

1 个答案:

答案 0 :(得分:1)

http://blog.bdoughan.com和stackoverflow挖掘后。

好的,我终于在stackoverflow上找到了这些

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    version="2.6.0">
    <java-types>
        <java-type name="com.abc.Parent" xml-transient="true" />
    </java-types>
</xml-bindings>

使用代码

    Map<String, Source> metadata = new HashMap<String,Source>();
    metadata.put("com.abc", new StreamSource( Volume.class.getClassLoader().getResourceAsStream("sample.xml"))); 
    Map<String,Object> properties = new HashMap<String,Object>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadata);
    JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Child.class}, properties);

然后可以在超类中获取/设置值。

如果您使用maven,本文可能会帮助您获取xmlbinding文件位置。

How do I solve EclipseLink's (MOXy) 'getting property "eclipselink.oxm.metadata-source" is not supported'?