所以这是我的功能:
public void writeUserToFile() {
File newXmlFile = new File("res\\SavedShoppers.xml");
try {
ArrayList<User> oldUsers = null;
if(newXmlFile.length() > 0){
oldUsers = buildUsersFromFile(); // get old users
// remove current user from old users list to eliminate duplicates
for (int i = 0; i < oldUsers.size(); i++) {
if (oldUsers.get(i).equals(this))
oldUsers.remove(i);
}
}
OutputStream outputStream = new FileOutputStream(newXmlFile);
XMLStreamWriter out = XMLOutputFactory.newInstance()
.createXMLStreamWriter(
new OutputStreamWriter(outputStream, "utf-8"));
out.writeStartDocument();
out.writeStartElement("user");
out.writeAttribute("userId", String.valueOf(getUserId()));
out.writeAttribute("name", getName());
for (ShoppingList sl : getShoppingLists()) {
out.writeStartElement("shoppingList");
out.writeAttribute("name", sl.getName());
for (ListItem li : sl.getItems()) {
out.writeStartElement("listItem");
out.writeAttribute("name", li.getName());
out.writeAttribute("quantity",
String.valueOf(li.getQuantity()));
out.writeEndElement();
}
out.writeEndElement(); // shoppingList
}
out.writeEndElement(); // user
out.writeEndDocument();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
所以我想要完成的是将用户保存到 XML 文件中以便以后提取。这是一个非常简单的购物清单应用程序,它将在您购物时创建一个购物车,以帮助您在购物时汇总您的购买。这是我得到的错误:
java.lang.NoClassDefFoundError:javax/xml/stream/XMLOutputFactory
当我调试时,这条线上正在发生:
XMLStreamWriter out = XMLOutputFactory.newInstance().createXMLStreamWriter(new OutputStreamWriter(outputStrem, "utf-8"))
而且我不确定我做错了什么......我已经为此工作了几天,所以我没有想法......任何帮助或指导都会很棒!
谢谢!乔恩