How to convert XML to Object and Object to XML

Description:
XMl to Java object and java object to xml creation using JAXB
So download
jaxb-api-2.2 jar



Code 1:
?
package com.kartik.xml.to.java;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class XmlToJava {
public static void main(String[] args) {  
  
    try {  
    //XMl to Object Convert
       File file = new File("d:\\question.xml");  
       JAXBContext jaxbContext = JAXBContext.newInstance(DTDC.class);  
  
       Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
       DTDC que= (DTDC) jaxbUnmarshaller.unmarshal(file); 
       
       System.out.println("Kartik Come Here"+que);
       
       JAXBContext contextObj = JAXBContext.newInstance(DTDC.class);  
       //Object To Xml Convert
       
       Marshaller marshallerObj = contextObj.createMarshaller();  
       marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
       try {
marshallerObj.marshal(que, new FileOutputStream("d:\\new_question.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}  
       
    } catch (JAXBException e) {  
        e.printStackTrace();  
      }  
}

}



Code 2:
?
package com.kartik.xml.to.java;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "DTDC")
public class DTDC {
private List<CNSTRACK> CNSTRACK;

/**
* @return the cNSTRACK
*/
public List<CNSTRACK> getCNSTRACK() {
return CNSTRACK;
}

/**
* @param cNSTRACK the cNSTRACK to set
*/
public void setCNSTRACK(List<CNSTRACK> cNSTRACK) {
CNSTRACK = cNSTRACK;


}



Code 3:
?
package com.kartik.xml.to.java;

import java.util.List;

public class CNSTRACK {
private String CNSNO;
List<TRANSDTL> TRANSDTL;
/**
* @return the cNSNO
*/
public String getCNSNO() {
return CNSNO;
}
/**
* @param cNSNO the cNSNO to set
*/
public void setCNSNO(String cNSNO) {
CNSNO = cNSNO;
}
/**
* @return the tRANSDTL
*/
public List<TRANSDTL> getTRANSDTL() {
return TRANSDTL;
}
/**
* @param tRANSDTL the tRANSDTL to set
*/
public void setTRANSDTL(List<TRANSDTL> tRANSDTL) {
TRANSDTL = tRANSDTL;
}
}



Code 4:
?
package com.kartik.xml.to.java;

public class TRANSDTL {
private String TRANSSTS;
private String TRANSNO;
private String TRANSORG;
private String TRANSDEST;
private String TRANSDATE;
private String TRANSTIME;
private String REMARKS;
private String TRANSORGCODE;
private String TRANSDESTCODE;
/**
 * @return the tRANSSTS
 */
public String getTRANSSTS() {
return TRANSSTS;
}
/**
 * @param tRANSSTS the tRANSSTS to set
 */
public void setTRANSSTS(String tRANSSTS) {
TRANSSTS = tRANSSTS;
}
/**
 * @return the tRANSNO
 */
public String getTRANSNO() {
return TRANSNO;
}
/**
 * @param tRANSNO the tRANSNO to set
 */
public void setTRANSNO(String tRANSNO) {
TRANSNO = tRANSNO;
}
/**
 * @return the tRANSORG
 */
public String getTRANSORG() {
return TRANSORG;
}
/**
 * @param tRANSORG the tRANSORG to set
 */
public void setTRANSORG(String tRANSORG) {
TRANSORG = tRANSORG;
}
/**
 * @return the tRANSDEST
 */
public String getTRANSDEST() {
return TRANSDEST;
}
/**
 * @param tRANSDEST the tRANSDEST to set
 */
public void setTRANSDEST(String tRANSDEST) {
TRANSDEST = tRANSDEST;
}
/**
 * @return the tRANSDATE
 */
public String getTRANSDATE() {
return TRANSDATE;
}
/**
 * @param tRANSDATE the tRANSDATE to set
 */
public void setTRANSDATE(String tRANSDATE) {
TRANSDATE = tRANSDATE;
}
/**
 * @return the tRANSTIME
 */
public String getTRANSTIME() {
return TRANSTIME;
}
/**
 * @param tRANSTIME the tRANSTIME to set
 */
public void setTRANSTIME(String tRANSTIME) {
TRANSTIME = tRANSTIME;
}
/**
 * @return the rEMARKS
 */
public String getREMARKS() {
return REMARKS;
}
/**
 * @param rEMARKS the rEMARKS to set
 */
public void setREMARKS(String rEMARKS) {
REMARKS = rEMARKS;
}
/**
 * @return the tRANSORGCODE
 */
public String getTRANSORGCODE() {
return TRANSORGCODE;
}
/**
 * @param tRANSORGCODE the tRANSORGCODE to set
 */
public void setTRANSORGCODE(String tRANSORGCODE) {
TRANSORGCODE = tRANSORGCODE;
}
/**
 * @return the tRANSDESTCODE
 */
public String getTRANSDESTCODE() {
return TRANSDESTCODE;
}
/**
 * @param tRANSDESTCODE the tRANSDESTCODE to set
 */
public void setTRANSDESTCODE(String tRANSDESTCODE) {
TRANSDESTCODE = tRANSDESTCODE;
}

}



Code 5: question.xml file
?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DTDC>
    <CNSTRACK>
        <CNSNO>700000603757</CNSNO>
        <TRANSDTL>
            <REMARKS>.</REMARKS>
            <TRANSDATE>15/10/2016</TRANSDATE>
            <TRANSDEST></TRANSDEST>
            <TRANSDESTCODE></TRANSDESTCODE>
            <TRANSNO>H3727545(CDS no)</TRANSNO>
            <TRANSORG>HYDERABAD- HYDERABAD</TRANSORG>
            <TRANSORGCODE>H84</TRANSORGCODE>
            <TRANSSTS>CDG</TRANSSTS>
            <TRANSTIME>00:00:00</TRANSTIME>
        </TRANSDTL>
    </CNSTRACK>
    <CNSTRACK>
        <CNSNO>700000603757</CNSNO>
        <TRANSDTL>
            <REMARKS>.</REMARKS>
            <TRANSDATE>15/10/2016</TRANSDATE>
            <TRANSDEST></TRANSDEST>
            <TRANSDESTCODE></TRANSDESTCODE>
            <TRANSNO>H3727545(CDS no)</TRANSNO>
            <TRANSORG>HYDERABAD- HYDERABAD</TRANSORG>
            <TRANSORGCODE>H84</TRANSORGCODE>
            <TRANSSTS>CDH</TRANSSTS>
            <TRANSTIME>00:00:00</TRANSTIME>
        </TRANSDTL>
    </CNSTRACK>
    <CNSTRACK>
        <CNSNO>700000603757</CNSNO>
        <TRANSDTL>
            <REMARKS>.</REMARKS>
            <TRANSDATE>15/10/2016</TRANSDATE>
            <TRANSDEST></TRANSDEST>
            <TRANSDESTCODE></TRANSDESTCODE>
            <TRANSNO>H3727545(CDS no)</TRANSNO>
            <TRANSORG>HYDERABAD- HYDERABAD</TRANSORG>
            <TRANSORGCODE>H84</TRANSORGCODE>
            <TRANSSTS>CDC</TRANSSTS>
            <TRANSTIME>00:00:00</TRANSTIME>
        </TRANSDTL>
    </CNSTRACK>
</DTDC>




Example 1 :




Previous
Next Post »

1 comments:

Click here for comments
20 November 2016 at 22:29 ×

Hi please view my articles and also share the your comments

Congrats bro Kartik Chandra Mandal you got PERTAMAX...! hehehehe...
Reply
avatar