Below is an example of how you could map your use case using EclipseLink JAXB (MOXy)'s @XmlPath
extension.
ExecRpt
You can specify the position of the element that you wish to map to @XmlPath("Pty[2]/@ID")
.
package forum12052961;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="ExecRpt")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1", "field2", "field3"})
public class ExecRpt {
@XmlPath("Pty[1]/@ID")
String field1;
@XmlPath("Instrmt/@Exch")
String field2;
@XmlPath("Pty[2]/@ID")
String field3;
}
jaxb.properties
To specify MOXy as your JAXB provider you need to have a file called jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum12052961;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ExecRpt.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12052961/input.xml");
ExecRpt execRpt = (ExecRpt) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(execRpt, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<ExecRpt>
<Pty ID="ABC"/>
<Instrmt Exch="AAA"/>
<Pty ID="ABD"/>
</ExecRpt>
For More Information