Created
February 16, 2015 09:32
-
-
Save uttesh/858742f46fbed397908d to your computer and use it in GitHub Desktop.
Jaxb model class
This file contains hidden or 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
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
@XmlRootElement | |
public class Customer { | |
String name; | |
String email; | |
int id; | |
public String getName() { | |
return name; | |
} | |
@XmlElement | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
@XmlElement | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public int getId() { | |
return id; | |
} | |
@XmlAttribute | |
public void setId(int id) { | |
this.id = id; | |
} | |
} |
This file contains hidden or 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
import java.io.File; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
public class JAXBMarshall { | |
public static void main(String[] args) { | |
Customer customer = new Customer(); | |
customer.setId(100); | |
customer.setName("uttesh"); | |
customer.setEmail("[email protected]"); | |
try { | |
File file = new File("customer.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); | |
Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); | |
// output pretty printed | |
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
jaxbMarshaller.marshal(customer, file); | |
jaxbMarshaller.marshal(customer, System.out); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or 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
import java.io.File; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Unmarshaller; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
public class JAXBUnMarshall { | |
public static void main(String[] args) { | |
try { | |
File file = new File("customer.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); | |
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); | |
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file); | |
System.out.println("Customer Name :"+customer.getName()); | |
System.out.println("Customer Email :"+customer.getEmail()); | |
System.out.println("Customer Id :"+customer.getId()); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment