Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. package jaxbTester;
  2.  
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.Writer;
  7. import java.util.ArrayList;
  8.  
  9. import javax.xml.bind.JAXBContext;
  10. import javax.xml.bind.JAXBException;
  11. import javax.xml.bind.Marshaller;
  12. import javax.xml.bind.Unmarshaller;
  13.  
  14.  
  15. public class Main {
  16.  
  17.     private static final String DATABASE = "./database.xml";
  18.  
  19.     public static void main(String[] args) throws JAXBException, IOException {
  20.  
  21.         ArrayList<Student> studentList = new ArrayList<Student>();
  22.  
  23.         Student student1 = new Student();
  24.         student1.setLastName("Langer");
  25.         student1.setFirstName("Heiner");
  26.         student1.setMat("8200896");
  27.         student1.setStud("Informatik");
  28.         student1.setEmail("heiner.langer@gmail.com");
  29.         studentList.add(student1);
  30.  
  31.         Student student2 = new Student();
  32.         student2.setLastName("Hoefer");
  33.         student2.setFirstName("Hanna");
  34.         student2.setMat("21000988");
  35.         student2.setStud("Elektrotechnik");
  36.         student2.setEmail("hanna.hoefer@gmail.com");
  37.        
  38.         studentList.add(student2);
  39.  
  40.         StudentDB studentdb = new StudentDB();
  41.         studentdb.setStudentsList(studentList);
  42.  
  43.         // create JAXB context and instantiate marshaller
  44.         JAXBContext context = JAXBContext.newInstance(StudentDB.class);
  45.         Marshaller m = context.createMarshaller();
  46.         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  47.         m.marshal(studentdb, System.out);
  48.  
  49.         Writer w = null;
  50.         try {
  51.             w = new FileWriter(DATABASE);
  52.             m.marshal(studentdb, w);
  53.         } finally {
  54.             try {
  55.                 w.close();
  56.             } catch (Exception e) {
  57.             }
  58.         }
  59.  
  60.         // get variables from our xml file, created before
  61.         System.out.println();
  62.         System.out.println("Output from our XML File: ");
  63.         Unmarshaller um = context.createUnmarshaller();
  64.         StudentDB studentdb2 = (StudentDB) um.unmarshal(new FileReader(
  65.                 DATABASE));
  66.  
  67.         for (int i = 0; i < studentdb2.getStudentsList().toArray().length; i++) {
  68.             System.out.println("Student " + (i + 1) + ": "
  69.                     + studentdb2.getStudentsList().get(i).getLastName() + " "
  70.                     + studentdb2.getStudentsList().get(i).getFirstName()+ " "
  71.                     + studentdb2.getStudentsList().get(i).getMat() + " "
  72.                     + studentdb2.getStudentsList().get(i).getStud() + " "
  73.                     + studentdb2.getStudentsList().get(i).getEmail() + "\n");
  74.         }
  75.  
  76.     }
  77. }
  78.  
  79. #####################################################################################
  80. #####################################################################################
  81. #####################################################################################
  82. package jaxbTester;
  83.  
  84. import java.io.FileReader;
  85. import java.io.FileWriter;
  86. import java.io.IOException;
  87. import java.io.Writer;
  88. import java.util.ArrayList;
  89.  
  90. import javax.xml.bind.JAXBContext;
  91. import javax.xml.bind.JAXBException;
  92. import javax.xml.bind.Marshaller;
  93. import javax.xml.bind.Unmarshaller;
  94.  
  95.  
  96. public class Main {
  97.  
  98.     private static final String DATABASE = "./database.xml";
  99.  
  100.     public static void main(String[] args) throws JAXBException, IOException {
  101.  
  102.         ArrayList<Student> studentList = new ArrayList<Student>();
  103.  
  104.         Student student1 = new Student();
  105.         student1.setLastName("Langer");
  106.         student1.setFirstName("Heiner");
  107.         student1.setMat("8200896");
  108.         student1.setStud("Informatik");
  109.         student1.setEmail("heiner.langer@gmail.com");
  110.         studentList.add(student1);
  111.  
  112.         Student student2 = new Student();
  113.         student2.setLastName("Hoefer");
  114.         student2.setFirstName("Hanna");
  115.         student2.setMat("21000988");
  116.         student2.setStud("Elektrotechnik");
  117.         student2.setEmail("hanna.hoefer@gmail.com");
  118.        
  119.         studentList.add(student2);
  120.  
  121.         StudentDB studentdb = new StudentDB();
  122.         studentdb.setStudentsList(studentList);
  123.  
  124.         // create JAXB context and instantiate marshaller
  125.         JAXBContext context = JAXBContext.newInstance(StudentDB.class);
  126.         Marshaller m = context.createMarshaller();
  127.         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  128.         m.marshal(studentdb, System.out);
  129.  
  130.         Writer w = null;
  131.         try {
  132.             w = new FileWriter(DATABASE);
  133.             m.marshal(studentdb, w);
  134.         } finally {
  135.             try {
  136.                 w.close();
  137.             } catch (Exception e) {
  138.             }
  139.         }
  140.  
  141.         // get variables from our xml file, created before
  142.         System.out.println();
  143.         System.out.println("Output from our XML File: ");
  144.         Unmarshaller um = context.createUnmarshaller();
  145.         StudentDB studentdb2 = (StudentDB) um.unmarshal(new FileReader(
  146.                 DATABASE));
  147.  
  148.         for (int i = 0; i < studentdb2.getStudentsList().toArray().length; i++) {
  149.             System.out.println("Student " + (i + 1) + ": "
  150.                     + studentdb2.getStudentsList().get(i).getLastName() + " "
  151.                     + studentdb2.getStudentsList().get(i).getFirstName()+ " "
  152.                     + studentdb2.getStudentsList().get(i).getMat() + " "
  153.                     + studentdb2.getStudentsList().get(i).getStud() + " "
  154.                     + studentdb2.getStudentsList().get(i).getEmail() + "\n");
  155.         }
  156.  
  157.     }
  158. }
  159.  
  160. ############################################################################
  161. ############################################################################
  162. ############################################################################
  163.  
  164. package jaxbTester;
  165. import java.util.ArrayList;
  166.  
  167. import javax.xml.bind.annotation.XmlElement;
  168. import javax.xml.bind.annotation.XmlElementWrapper;
  169. import javax.xml.bind.annotation.XmlRootElement;
  170.  
  171. @XmlRootElement(namespace = "wba.gruppe05.org")
  172. public class StudentDB {
  173.  
  174.     // XmLElementWrapper generates a wrapper element around XML representation
  175.     @XmlElementWrapper(name = "studentDB")
  176.     // XmlElement sets the name of the entities
  177.     @XmlElement(name = "Student")
  178.     private ArrayList<Student> studentList;
  179.  
  180.  
  181.     public void setStudentsList(ArrayList<Student> studentList) {
  182.         this.studentList = studentList;
  183.     }
  184.  
  185.     public ArrayList<Student> getStudentsList() {
  186.         return studentList;
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement