2012-11-11 41 views
2

我是JAXB的新手,所以請在您的評論中留下餘地!無論如何,我每次運行該程序時都會遇到我現有的xml文件被覆蓋的問題,而這並不是我想要的。這是假設添加到現有的XML。請幫忙!Java/Jaxb幫助不需要覆蓋現有文件

CoursesApp.java:

package Courses; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

public class CoursesApp { 
    public static void main(String[] args) { 
     int choice; 
     String courseCode = "", professorName = "", groupIndex = "", classType = ""; 
     Scanner sc = new Scanner(System.in); 

     try { 

      File file = new File("C:\\Courselist.xml"); 
      JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class); 

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      Courselist courselist = (Courselist) jaxbUnmarshaller.unmarshal(file); 
      System.out.println(courselist.course.get(0).getclassType()); 

      } catch (JAXBException e) { 
      e.printStackTrace(); 
      } 

     Courselist courselist = new Courselist(); 
     Course course = new Course(); 

     do{ 
      System.out.println("(1) Add a student.\n" + 
           "(2) Add a course.\n" + 
           "(3) Exit.\n"); 
      System.out.print("Enter the number of your choice: "); 
      choice = sc.nextInt(); 
      sc.nextLine(); 
      switch (choice) { 
      case 1: 
        break; 
      case 2: 
       System.out.println("Please enter course code:"); 
       courseCode = sc.nextLine(); 
       System.out.println("Please enter class type:"); 
       classType = sc.nextLine(); 
       System.out.println("Please enter group index:"); 
       groupIndex = sc.nextLine(); 
       System.out.println("Please enter professor name:"); 
       professorName = sc.nextLine(); 
       course.setcourseCode(courseCode); 
       course.setclassType(classType); 
       course.setgroupIndex(groupIndex); 
       course.setprofessor(professorName); 
       courselist.course.add(course); 
       try{ 
        File file = new File("C:\\Courselist.xml"); 
        JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class); 
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

        jaxbMarshaller.marshal(courselist, file); 
        jaxbMarshaller.marshal(courselist, System.out); 

       }catch(JAXBException e) 
       { 
        e.printStackTrace(); 
       } 
        break; 
      case 3: //Modify course 
        break; 
      default: System.out.println("Please enter a number between 1-3.\n"); 
        break; 
      } 
     } while (choice!=3); 
    } 
} 

Courselist.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<courselist> 
    <course> 
     <classType>lect</classType> 
     <courseCode>2002</courseCode> 
     <groupIndex>12</groupIndex> 
     <professor>james</professor> 
    </course> 
</courselist> 

下面的一個,如果我添加其他課程:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<courselist> 
    <course> 
     <classType>lect</classType> 
     <courseCode>2002</courseCode> 
     <groupIndex>12</groupIndex> 
     <professor>james</professor> 
    </course> 
    <course> 
     <classType>Lab</classType> 
     <courseCode>2001</courseCode> 
     <groupIndex>1</groupIndex> 
     <professor>john</professor> 
    </course> 
</courselist> 
+0

只要讀取文件,將對象添加到您讀取的集合中並將其保存回來。 – toniedzwiedz

回答

3

以爲我已經回答了這一點,但答案消失了

首先不這樣做:

 } catch (JAXBException e) { 
     e.printStackTrace(); 
     } 

你捕捉異常,傾銷文本到stdout,然後繼續好像什麼都沒發生。這是由IDE添加的無用的樣板,不這樣做

由於您想調整您的行爲是否已存在文件,因此我建議使用if塊並結合使用File.exists

2
import java.io.File; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.Scanner; 

    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javax.xml.bind.JAXBContext; 
    import javax.xml.bind.JAXBException; 
    import javax.xml.bind.Marshaller; 
    import javax.xml.bind.Unmarshaller; 

    public class CoursesApp { 

     int choice; 
     String courseCode = "", professorName = "", groupIndex = "", classType = ""; 
     private boolean exists; 
     Courselist courselist = new Courselist(); 
     Course course = new Course(); 

     public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in); 
      Courselist courselist = new Courselist(); 
      Course course = new Course(); 
      try { 

       File file = new File("C:\\Courselist.xml"); 
       JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class); 

       Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
       courselist = (Courselist) jaxbUnmarshaller.unmarshal(file); 
       System.out.println(courselist); 

      } catch (JAXBException e) { 
       e.printStackTrace(); 
      } 
      //* OVER HERE *// 
      CoursesApp ca = new CoursesApp(); 
      ca.process(0); 
     } 

     private void process(int choice) { 

      //* PRINT MENU *// 
      System.out.println("(1) Add a student.\n" 
        + "(2) Add a course.\n" 
        + "(3) Exit.\n"); 
      System.out.println("Enter the number of your choice: "); 
      Scanner sc = new Scanner(System.in); 
      choice = sc.nextInt(); 
      sc.nextLine(); 
      switch (choice) { 
       case 1: 
        //ADD STUDENTS 
        break; 
       case 2: 
        File file = new File("C:\\Course.xml"); 
        if (file.exists()) { 
         choice = 4; 
         exists = true; 
        } else { 
         System.out.println("File doesnot exist press 4 to create or 3 to exit: "); 
         choice = sc.nextInt(); 
         sc.nextLine(); 
        } 
        /*ENTER COURSE DETAILS*/ 
        if (choice == 4) { 
         System.out.println("Please enter course code:"); 
         courseCode = sc.nextLine(); 
         System.out.println("Please enter class type:"); 
         classType = sc.nextLine(); 
         System.out.println("Please enter group index:"); 
         groupIndex = sc.nextLine(); 
         System.out.println("Please enter professor name:"); 
         professorName = sc.nextLine(); 
         course.setcourseCode(courseCode); 
         course.setclassType(classType); 
         course.setgroupIndex(groupIndex); 
         course.setprofessor(professorName); 
         courselist.course.add(course); 
         System.out.println("Are there more records?\n Press 2(yes) or 3(n): "); 
         choice = sc.nextInt(); 
         sc.nextLine(); 
         if (choice == 3) { 
          /*END OF RECORDS SO GO AHEAD AND UNMARSHAL*/ 
          unmarshal(file, courselist); 
         } else { 
          /*ADD MORE COURSE ITEMS*/ 
          process(2); 
         } 
        } else { 
         /*FILE NOT EXIST AND USER SELECTED EXIT*/ 
         System.exit(0); 
         break; 
        } 
        break; 
       case 3: 
        System.exit(0); 
        break; 
      } 
     } 

     private void unmarshal(File file, Courselist courselist) { 
      { 
       FileWriter fw = null; 
       try { 
        JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class); 
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
        StringBuffer str = new StringBuffer(); 
        /*EXISTING FILE SO APPEND*/ 
        if (exists) { 
         fw = new FileWriter(file, true); 
         jaxbMarshaller.marshal(courselist, fw); 
        } 
        /*CREATE NEW FILE*/ 
        else { 
         jaxbMarshaller.marshal(courselist, file); 
        } 
        jaxbMarshaller.marshal(courselist, System.out); 
       } catch (IOException ex) { 
        Logger.getLogger(CoursesApp.class.getName()).log(Level.SEVERE, null, ex); 
       } catch (JAXBException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         fw.close(); 
        } catch (IOException ex) { 
         Logger.getLogger(CoursesApp.class.getName()).log(Level.SEVERE, null, ex); 
        } 
       } 
      } 
      process(0);//call and print options 
     } 
    } 

這將解決覆蓋問題,但您需要一種方法來防止xml聲明的重複。希望這可以幫助

我基本上採用了提問者發佈的代碼,並提取了一些代碼來分離方法以更好地理解。我還添加了檢查以查看文件是否存在並追加到文件而不是覆蓋內容。

Process()方法用於用戶輸入和需要執行的操作。在unmarshal()中,用戶輸入綁定到xml文件。

+0

假設這有效(我不知道)。這有什麼啓發作爲答案?至少,你所做的關於這個問題的一些評論會很有用。 – eh9