我有一个俱乐部成员的注册表,我通过代码将成员保存到xml文件时取得了成功:
public void saveToRegistry() throws IOException {
if (memberList.isEmpty()) {
System.err.println("Sorry, you do not have any members in the Registry to Save!");
}
else {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
try {
JAXBContext context;
context = JAXBContext.newInstance(Member.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
for (int i = 0; i < memberList.size(); i++) {
marshaller.marshal(memberList.get(i), out);
}
System.out.print("Your members have successfully been Saved in C:\\Users");
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
我的会员班是:
@XmlRootElement(name = "Member")
@XmlType(propOrder = {"name", "persNum", "id", "numOfBoats", "boats", "boat"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Member {
@XmlElement(name = "MemberName")
private String name;
@XmlElement(name = "PersonalNumber")
private String persNum;
@XmlElement(name = "IdentityNumber")
private String id;
@XmlElement(name = "NumberofBoats")
private int numOfBoats;
@XmlElement(name = "BoatInfo")
private ArrayList<Boat> boats = new ArrayList<Boat>();
@XmlElement(name = "Boat")
private Boat boat;
public Member() {}
public Member(String memberName, String memberPersNum) {
this.name = memberName;
this.persNum = memberPersNum;
}
//Member ID is the first letter of the name plus the last three numbers of their personal number.
public String createID() {
id = getName().substring(0, 1) + getPersNum().substring(7, 10); //needs a dash when making the personal number
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPersNum() {
return persNum;
}
public void setPersNum(String persNum) {
this.persNum = persNum;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNumOfBoats() {
return numOfBoats;
}
public void setNumOfBoats(int numOfBoats) {
this.numOfBoats = numOfBoats;
}
public Boat getBoat() {
return boat;
}
public void setBoat(Boat boat) {
this.boat = boat;
}
public String toString(){
String temp =
"\nMember: " + this.name + " "
+ "\nMember ID: " + this.id
+ " " + "\nPersonal Number: " + this.persNum
+ " " + "\nNumber of Boats: " + this.numOfBoats
+"\n";
return temp;
}
}
但是,当尝试使用代码从文件加载成员时:
public void loadFromRegistry() throws FileNotFoundException, JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Member.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Member emps = (Member) jaxbUnmarshaller.unmarshal(file);
for (Member mem : getMembers()) {
memberList.add(mem);
System.out.println(mem.getName());
System.out.println(mem.getPersNum());
}
}
public ArrayList<Member> getMembers() {
return memberList;
}
我的输出错误提示:
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; systemId: file:/C:/Users/Pranav/Member%20Registry.txt; lineNumber: 8; columnNumber: 6; The processing instruction target matching "[xX][mM][lL]" is not allowed.]
如果有人能说出我做错了什么,我们将不胜感激。