Note. I am an EclipseLink JAXB (MOXy) and a member of the JAXB Group (JSR-222) .
Below I will demonstrate how you could match your use case with the EclipseLink JAXB (MOXy) @XmlPath .
CNBody
package forum12934737; import java.util.List; import javax.xml.bind.annotation.*; @XmlRootElement(name="CNBODY") public class CNBody { @XmlElement(name="CNACTIONTRACK") private String cnActionTrack; @XmlElement(name="CNACTION") private List<CNAction> cnActions; }
CNAction
Using the @XmlPath extension, we can specify that we want to map our field / property to an XML attribute nested in an XML element that has a different XML attribute of a specific value.
package forum12934737; import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlAccessorType(XmlAccessType.FIELD) public class CNAction { @XmlPath("FIELD[@NAME='strAction']/@VALUE") private String strAction; @XmlPath("FIELD[@NAME='strRemarks']/@VALUE") private String strRemarks; }
jaxb.properties
To specify MOXy as the JAXB provider, you need to include a file named jaxb.properties in the same package as your domain model, with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum12934737; import java.io.File; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(CNBody.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum12934737/input.xml"); CNBody cnBody = (CNBody) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(cnBody, System.out); } }
Input.xml / output
For this example, I am using a subset of your XML document.
<CNBODY> <CNACTIONTRACK>TRUE</CNACTIONTRACK> <CNACTION> <FIELD NAME="strAction" VALUE="DISPATCHED"/> <FIELD NAME="strRemarks" VALUE=""/> </CNACTION> <CNACTION> <FIELD NAME="strAction" VALUE="RECEIVED"/> <FIELD NAME="strRemarks" VALUE=""/> </CNACTION> <CNACTION> <FIELD NAME="strAction" VALUE="DISPATCHED"/> <FIELD NAME="strRemarks" VALUE=""/> </CNACTION> <CNACTION> <FIELD NAME="strAction" VALUE="RECEIVED"/> <FIELD NAME="strRemarks" VALUE=""/> </CNACTION> <CNACTION> <FIELD NAME="strAction" VALUE="OUT FOR DELIVERY"/> <FIELD NAME="strRemarks" VALUE=""/> </CNACTION> </CNBODY>
Additional Information
source share