我是jaxb的新手,所以请在您的评论中宽容!无论如何,每次运行程序时,我都面临着我现有的XML文件的问题,这不是我想要的。它可以添加到现有的XML中。请帮助!
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>
以为我已经回答了,但是答案消失了...
首先不这样做:
} catch (JAXBException e) {
e.printStackTrace();
}
您正在捕获例外,将文本丢给Stdout,然后继续,好像什么都没发生。这是由IDE添加的无用的样板,不这样做。
,由于您想调节您的行为是否已经存在,因此我建议将if
块与文件使用。
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声明的方法。希望这有帮助
我从本质上将Asker发布的代码和提取的代码零件提取到分开方法以更好地理解。我还添加了检查,以查看文件是否存在并附加到文件上,而不是覆盖内容。
process()方法用于用户输入,需要执行操作。在Unmarshal()中,用户输入绑定到XML文件。
您需要使用jaxb 2.x binder 抽象。详细的教程。