好的,所以我正在创建一个用户类,它要求输入,然后使用 java 将其存储在 XML 文件中。我想创建我认为的原始 XML 文件,但我无法弄清楚如何添加一个新用户,其属性“id”比上一个用户条目多一个。
这是我到目前为止的代码:
/*imports */
public class CreateUser {
static Scanner input = new Scanner(System.in);
/* object names*/
String name;
String age;
String bday;
String gender;
String location;
String orientation;
String relationship;
String hobbies;
String choice;
String username;
String password;
public void makeUser(){
/*left out code to get user entries here
seemed irrelevant/*
/*checks for file if it doesn't exist then it creates it else it should append
the user to the xml document with a id increase of one.
The appending part I'm not sure how to do.*/
File f = new File("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml");
if(f.exists()) {
try {
/* need help here*/
}
}
else{
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document users = docBuilder.newDocument();
Element user = users.createElement("user");
users.appendChild(user);
Attr attr = users.createAttribute("id");
attr.setValue("0");
user.setAttributeNode(attr);
Element dname = users.createElement("name");
dname.appendChild(users.createTextNode(name));
user.appendChild(dname);
Element dgender = users.createElement("gender");
dgender.appendChild(users.createTextNode(gender));
user.appendChild(dgender);
Element dlocation = users.createElement("location");
dlocation.appendChild(users.createTextNode(location);
user.appendChild(dlocation);
Element dorientation = users.createElement("orientation");
dorientation.appendChild(users.createTextNode(orientation));
user.appendChild(dorientation);
Element drelationship = users.createElement("relationship");
drelationship.appendChild(users.createTextNode(relationship));
drelationship.appendChild(drelationship);
Element dhobbies = users.createElement("hobbies");
dhobbies.appendChild(users.createTextNode(hobbies));
dhobbies.appendChild(dhobbies);
Element dchoice = users.createElement("choice");
dchoice.appendChild(users.createTextNode(choice));
dchoice.appendChild(dchoice);
Element dusername = users.createElement("username");
dusername.appendChild(users.createTextNode(username));
dusername.appendChild(dusername);
Element dpassword = users.createElement("password");
dpassword.appendChild(users.createTextNode(password));
dpassword.appendChild(dpassword);
Element dbday = users.createElement("birthday");
dbday.appendChild(users.createTextNode(bday));
dbday.appendChild(dbday);
Element dage = users.createElement("age");
dage.appendChild(users.createTextNode(age));
dage.appendChild(dage);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(users);
StreamResult result = new StreamResult(new File("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml"));
StreamResult test = new StreamResult(System.out);
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
我知道它有很多代码要查看,我不想要一个精确的编码答案,但也许只是如何将属性值附加到上一个条目之后的用户。或者指向一个有用的网站的方向。任何事情真的让我有点困惑,我觉得我应该得到这么简单的东西。提前感谢您的帮助