Last active
August 1, 2020 17:57
-
-
Save jx13xx/0c897010afe53a6a099c91a7b57ef474 to your computer and use it in GitHub Desktop.
Create a Marshalling program on Java which converts objects into data format suitable for storage and transmission. Included with the POJO Class (Getters & Setters methods)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement(name ="products") | |
public class ListProduct { | |
private List<Product> listProduct = new ArrayList<Product>(); | |
@XmlElement(name ="product") | |
public List<Product> getListProduct() { | |
return listProduct; | |
} | |
public void setListProduct(List<Product> listProduct) { | |
this.listProduct = listProduct; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import javax.xml.bind.JAXBException; | |
public class Main { | |
public static void main(String[] args) throws JAXBException { | |
// write your code here | |
System.out.println("Hi There!"); | |
ProductJAXB p = new ProductJAXB(); | |
long a = System.currentTimeMillis(); | |
p.MarshallList(); | |
System.out.println(":::::::::::::::"+(System.currentTimeMillis() - a)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement(name ="product") | |
public class Product { | |
public Product() { | |
super(); | |
} | |
private String countryCode; | |
private int telephone; | |
public Product(String countryCode, int telephone) { | |
super(); | |
this.countryCode = countryCode; | |
this.telephone = telephone; | |
} | |
@XmlElement | |
public String getCountryCode() { | |
return countryCode; | |
} | |
public void setCountryCode(String countryCode) { | |
this.countryCode = countryCode; | |
} | |
@XmlElement | |
public int getTelephone() { | |
return telephone; | |
} | |
public void setTelephone(int telephone) { | |
this.telephone = telephone; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
public class ProductJAXB { | |
JAXBContext jc = null; | |
public ProductJAXB() { | |
try { | |
jc=JAXBContext.newInstance(ListProduct.class); | |
} catch (JAXBException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public void MarshallList() throws JAXBException { | |
List<Product> listProduct = new ArrayList<Product>(); | |
// Product product = new Product("AE", 971); | |
JAXBContext jc = JAXBContext.newInstance(ListProduct.class); | |
Marshaller ms = jc.createMarshaller(); | |
try { | |
listProduct.add(new Product("AE", 971)); | |
listProduct.add(new Product("AF", 93)); | |
listProduct.add(new Product("AL", 355)); | |
listProduct.add(new Product("A0", 244)); | |
ListProduct lp = new ListProduct(); | |
lp.setListProduct(listProduct); | |
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
ms.marshal(lp, System.out); | |
ms.marshal(lp, new File("Product.xml")); | |
//System.exit(0); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public void unMarshall() { | |
try { | |
JAXBContext jc = JAXBContext.newInstance(ListProduct.class); | |
Unmarshaller ums = jc.createUnmarshaller(); | |
ListProduct lp = (ListProduct)ums.unmarshal(new File ("Product.xml")); | |
if (lp == null) { | |
System.out.println("Empty"); | |
}else { | |
for(Product p : lp.getListProduct() ) { | |
System.out.println("Product Information" ); | |
System.out.println("Country Code " + p.getCountryCode()); | |
System.out.println("Phone" + p.getTelephone()); | |
} | |
} | |
}catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment