JAXB unmarshall for several

I was trying to figure out if it is possible to undo an xml element for multiple pojos. eg:

for xml:

<type> <id>1</id> <cost>12</cost> <height>15</height> <width>13</width> <depth>77</depth> </type> 

Item Class

 @XmlAccessorType(XmlAccessType.PROPERTY) @XmlRootElement(name="type") public class Item { private Integer id; private Double cost; @XmlElement(name="id") public Integer getId(){ return id; } @XmlElement(name="cost") public Double getCost(){ return cost } } 

Class ItemDimensions

 @XmlAccessorType(XmlAccessType.PROPERTY) @XmlRootElement(name="type") public class ItemDimensions { private Integer height; private Integer width; private Integer depth; @XmlElement(name="height") public Integer getHeight(){ return height; } @XmlElement(name="width") public Integer getWidth(){ return width; } @XmlElement(name="depth") public Integer getDepth(){ return depth; } } 

I tried to do something similar using a series of JAXB mappings created by Netbeans 6.9, and a series of test classes, but now they got it. Does anyone know if this can be done without any intermediate objects?

+4
source share
1 answer

You can use the @XmlPath extension in EclipseLink JAXB (MOXy) to fulfill this use case (I am the leader of MOXy tech):

Root

JAXB requires one object to be unmarshal, we will introduce a class to fulfill this role. This class will have fields corresponding to two objects that you want to decouple with annotation using your own XPath: @XmlPath (".")

 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement(name="type") @XmlAccessorType(XmlAccessType.FIELD) public class Root { @XmlPath(".") private Item item; @XmlPath(".") private ItemDimensions itemDimensions; } 

ItemDimensions

You usually comment on this class. In your example, you are commenting on properties, but only providing getters. This will make JAXB think these are write-only records.

 import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class ItemDimensions { private Integer height; private Integer width; private Integer depth; } 

Item

 import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class Item { private Integer id; private Double cost; } 

Demo

 import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Unmarshaller u = jc.createUnmarshaller(); Object o = u.unmarshal(new File("input.xml")); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(o, System.out); } } 

jaxb.properties

To use MOXy as your JAXB implementation, you must provide a file named jaxb.properties in your domain objects with the following entry:

 javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 
+2
source

All Articles