1

When use the Moxy to unmarshaling the sample xml to child, it always can't get the name. It always is null.

Sample xml

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

Sample class

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"));

How can I get this value, if I can't make any change on the Parent class.

Thanks,

Rao
  • 20,781
  • 11
  • 57
  • 77
JinruiDu
  • 353
  • 1
  • 2
  • 9

1 Answers1

1

After dig with http://blog.bdoughan.com and stackoverflow.

OK, I finally found these on 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>

With Code

    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);

Then could get/set the value in superclass.

If you use the maven this article may help you for the xmlbinding file location.

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

Community
  • 1
  • 1
JinruiDu
  • 353
  • 1
  • 2
  • 9