JAXB / MOXy - not unmarshalling values after ignored elements [message #1112048] |
Thu, 19 September 2013 04:04  |
Eclipse User |
|
|
|
I'm working on a JAXB/MOXy XML parsing implementation using EclipseLink v2.1.3 (old, I know but we're still tied to Java 5) and I've encountered an issue with unmarshalling.
Part of the document has the following XML:
<Product>
<Code>code</Code>
<Desc>description</Desc>
</ProductInfo>
The object I'm parsing this into has the following field and annotation:
@XmlElement(name = "Product/Desc")
private String description;
That is, it tries to extract the "Desc" value, but doesn't care about the "Code". I'm finding that the description field is null after unmarshalling the XML. However, if I add in a field to extract "Code":
@XmlElement(name = "Product/Code")
private String code;
then description field is not null, and contains the correct value - both values get populated.
I've found the same issue elsewhere in the XML and it seems to be that if I try to skip / ignore certain XML elements, values after that skipped elements are also ignored.
Is this a known issue, and is there a workaround for it?
|
|
|
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112515 is a reply to message #1112048] |
Thu, 19 September 2013 20:38   |
Eclipse User |
|
|
|
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>
|
|
|
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112584 is a reply to message #1112515] |
Thu, 19 September 2013 22:57   |
Eclipse User |
|
|
|
Thanks for the response Blaise - I had tried @XmlPath as well, and it had the same behaviour.
After I'd posted this yesterday, I found that I was experiencing this bug. I've been using the following earlier in my object structure:
Once I removed this, the issue disappeared. Our model is still in flux, so was simple enough to change how we were populating the object that used the self-element.
|
|
|
|
Powered by
FUDForum. Page generated in 0.02676 seconds