Using JAXB we can read/write the multiple xml nodes.
consider a scenario where u have to read the list of items from the file system, then its better option is to use JSON or XML structure.
Full sample of the JAXB reading the list of object from the xml file.
consider a scenario where u have to read the list of items from the file system, then its better option is to use JSON or XML structure.
Full sample of the JAXB reading the list of object from the xml file.
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
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 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("uttesh@gmail.com"); | |
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 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(); | |
} | |
} | |
} |
0 comments:
Post a Comment