|
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112515 is a reply to message #1112048] |
Thu, 19 September 2013 20:38 |
|
Instead of use @XmlElement, you should leverage the @XmlPath annotation we introduced in EclipseLink 2.1 as follows.
Domain Model
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlPath("Product/Code/text()")
private String code;
@XmlPath("Product/Desc/text()")
private String description;
}
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<Product>
<Code>code</Code>
<Desc>description</Desc>
</Product>
</foo>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03462 seconds