Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jaxbTester;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.ArrayList;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.Unmarshaller;
- public class Main {
- private static final String DATABASE = "./database.xml";
- public static void main(String[] args) throws JAXBException, IOException {
- ArrayList<Student> studentList = new ArrayList<Student>();
- Student student1 = new Student();
- student1.setLastName("Langer");
- student1.setFirstName("Heiner");
- student1.setMat("8200896");
- student1.setStud("Informatik");
- student1.setEmail("heiner.langer@gmail.com");
- studentList.add(student1);
- Student student2 = new Student();
- student2.setLastName("Hoefer");
- student2.setFirstName("Hanna");
- student2.setMat("21000988");
- student2.setStud("Elektrotechnik");
- student2.setEmail("hanna.hoefer@gmail.com");
- studentList.add(student2);
- StudentDB studentdb = new StudentDB();
- studentdb.setStudentsList(studentList);
- // create JAXB context and instantiate marshaller
- JAXBContext context = JAXBContext.newInstance(StudentDB.class);
- Marshaller m = context.createMarshaller();
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
- m.marshal(studentdb, System.out);
- Writer w = null;
- try {
- w = new FileWriter(DATABASE);
- m.marshal(studentdb, w);
- } finally {
- try {
- w.close();
- } catch (Exception e) {
- }
- }
- // get variables from our xml file, created before
- System.out.println();
- System.out.println("Output from our XML File: ");
- Unmarshaller um = context.createUnmarshaller();
- StudentDB studentdb2 = (StudentDB) um.unmarshal(new FileReader(
- DATABASE));
- for (int i = 0; i < studentdb2.getStudentsList().toArray().length; i++) {
- System.out.println("Student " + (i + 1) + ": "
- + studentdb2.getStudentsList().get(i).getLastName() + " "
- + studentdb2.getStudentsList().get(i).getFirstName()+ " "
- + studentdb2.getStudentsList().get(i).getMat() + " "
- + studentdb2.getStudentsList().get(i).getStud() + " "
- + studentdb2.getStudentsList().get(i).getEmail() + "\n");
- }
- }
- }
- #####################################################################################
- #####################################################################################
- #####################################################################################
- package jaxbTester;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.ArrayList;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.Unmarshaller;
- public class Main {
- private static final String DATABASE = "./database.xml";
- public static void main(String[] args) throws JAXBException, IOException {
- ArrayList<Student> studentList = new ArrayList<Student>();
- Student student1 = new Student();
- student1.setLastName("Langer");
- student1.setFirstName("Heiner");
- student1.setMat("8200896");
- student1.setStud("Informatik");
- student1.setEmail("heiner.langer@gmail.com");
- studentList.add(student1);
- Student student2 = new Student();
- student2.setLastName("Hoefer");
- student2.setFirstName("Hanna");
- student2.setMat("21000988");
- student2.setStud("Elektrotechnik");
- student2.setEmail("hanna.hoefer@gmail.com");
- studentList.add(student2);
- StudentDB studentdb = new StudentDB();
- studentdb.setStudentsList(studentList);
- // create JAXB context and instantiate marshaller
- JAXBContext context = JAXBContext.newInstance(StudentDB.class);
- Marshaller m = context.createMarshaller();
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
- m.marshal(studentdb, System.out);
- Writer w = null;
- try {
- w = new FileWriter(DATABASE);
- m.marshal(studentdb, w);
- } finally {
- try {
- w.close();
- } catch (Exception e) {
- }
- }
- // get variables from our xml file, created before
- System.out.println();
- System.out.println("Output from our XML File: ");
- Unmarshaller um = context.createUnmarshaller();
- StudentDB studentdb2 = (StudentDB) um.unmarshal(new FileReader(
- DATABASE));
- for (int i = 0; i < studentdb2.getStudentsList().toArray().length; i++) {
- System.out.println("Student " + (i + 1) + ": "
- + studentdb2.getStudentsList().get(i).getLastName() + " "
- + studentdb2.getStudentsList().get(i).getFirstName()+ " "
- + studentdb2.getStudentsList().get(i).getMat() + " "
- + studentdb2.getStudentsList().get(i).getStud() + " "
- + studentdb2.getStudentsList().get(i).getEmail() + "\n");
- }
- }
- }
- ############################################################################
- ############################################################################
- ############################################################################
- package jaxbTester;
- import java.util.ArrayList;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlElementWrapper;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(namespace = "wba.gruppe05.org")
- public class StudentDB {
- // XmLElementWrapper generates a wrapper element around XML representation
- @XmlElementWrapper(name = "studentDB")
- // XmlElement sets the name of the entities
- @XmlElement(name = "Student")
- private ArrayList<Student> studentList;
- public void setStudentsList(ArrayList<Student> studentList) {
- this.studentList = studentList;
- }
- public ArrayList<Student> getStudentsList() {
- return studentList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement