I have a slight of a problem. I am doing a simple member system over a club. In the system, you should be able to register a new user, and register the users boats. I then save it to a .xml file using JDOM. I have followed a guide in the following site: http://www.journaldev.com/1211/jdom-write-xml-file-example-from-object
The problem is that when a call the method that adds the member in the xml file, it writes a new file. Yes for one time it isn't a problem, but to be able to add members after members even after turning the application off, it will be overwritten and the new member takes the place. This is not what i want.
I want it somehow that every time i add a new member the member gets added at the end of the last added element in the existing xml file, is the no file, then it creates the file or something. Would appreciate help from more experienced guys like you;)
<?xml version="1.0" encoding="UTF-8"?>
<Members xmlns="boatclubsystem members">
<Member xmlns="" memberId="null">
<name>Jakob Wångö</name>
<personalID>199107270077</personalID>
<Boat>
<boatType>Sailingboat</boatType>
<boatLenght>30m</boatLenght>
</Boat>
</Member>
<Member> xmlns ="memberID="null">
<name>John Doe</name>
</Member>
</Members>
public class Controller{
View theView;
Member memberModel;
Boat boatModel;
static String file = "BoatClubSystem.xml";
static List<Member> memberlist = new ArrayList<Member>();
static List<Boat> boatlist = new ArrayList<Boat>();
public static void main(String[]args) throws IOException{
Member m = new Member();
Member m2 = new Member();
Boat b = new Boat();
Boat b2 = new Boat();
m.setName("Jakob Wangoe");
m.setPersonID("19910727****");
memberlist.add(m);
b.setBoatType(1);
b.setBoatLength(30);
boatlist.add(b);
createXMLSystem(memberlist, boatlist);
}
public static void createXMLSystem(List<Member> memberList, List<Boat> boatlist) throws IOException{
Document doc = new Document();
doc.setRootElement(new Element("Members",
Namespace.getNamespace("boatclubsystem members")));
for (Member memb : memberList){
Element member = new Element("Member");
member.addContent(new Element("memberID").setText(memb.getMemberID()));
member.addContent(new Element("name").setText(""+memb.getName()));
member.addContent(new Element("personalID").setText(memb.getPersonID()));
for(Boat boat : boatlist){
Element boats = new Element("Boat");
boats.addContent(new Element("boatType").setText(boat.getBoatType()));
boats.addContent(new Element("boatLenght").setText(boat.getBoatLenght()+"m"));
member.addContent(boats);
}
doc.getRootElement().addContent(member);
}
//JDOM document is ready, write to file
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
xmlOutputter.output(doc, new FileOutputStream(file));
}
}