XML having the name and value of a field in response to a java map object that cannot be retrieved from fields

I have a lower answer as xml. I need to match a java object that I tried using jaxb, sax, dom, could not get the data below the <FIELD NAME="strShipmentNo" VALUE="A134535" /> data. Please suggest how to get the data below xml

 <DTDCREPLY> <CONSIGNMENT> <CNHEADER> <CNTRACK>TRUE</CNTRACK> <FIELD NAME="strShipmentNo" VALUE="A15082271" /> <FIELD NAME="strRefNo" VALUE="N/A" /> <FIELD NAME="strMode" VALUE="AIR" /> <FIELD NAME="strOrigin" VALUE="LEAK-PROOF ENGINEERING PVT.LTD, AHMEDABAD" /> <FIELD NAME="strOriginRemarks" VALUE="Received from" /> <FIELD NAME="strBookedOn" VALUE="08072009" /> <FIELD NAME="strPieces" VALUE="1" /> <FIELD NAME="strWeightUnit" VALUE="Kg" /> <FIELD NAME="strWeight" VALUE="0.020" /> <FIELD NAME="strDestination" VALUE="PUNE" /> <FIELD NAME="strStatus" VALUE="DELIVERY" /> <FIELD NAME="strStatusTransOn" VALUE="09072009" /> <FIELD NAME="strStatusTransTime" VALUE="1210" /> <FIELD NAME="strRemarks" VALUE="CO SEAL" /> <FIELD NAME="strNoOfAttempts" VALUE="" /> </CNHEADER> <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> </CONSIGNMENT> </DTDCREPLY> 
+4
source share
2 answers

Using XStream , you can serialize most Java objects without any mapping. The names of the objects become the names of the elements in the generated XML, and the lines inside the classes form the contents of the XML element. Classes that you serialize with XStream should not implement the Serializable interface. XStream is a serialization tool, not a data binding tool, which means that it does not generate classes from an XML file or XML Schema (XSD). Check out this stackoverflow question

+1
source

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

0
source

Source: https://habr.com/ru/post/1440293/


All Articles