I'm trying to write a list of elements to a .txt file using XMLOutputter and I'm stumped. I set my program up to retrieve the currentElement [currentElement = (Element)listIterator.next();] every time the user clicks a button. Then they are asked if they want to write the currentElement to the .txt file and what should happen is that each time the user clicks yes, the currentElement should be written one after the other to the file like so:
<br><br><word_list><br> <title name ="words" /><br> <entry word = "currentword1" ><br> <def>currentdef1</def><br> </entry><br> <entry word = "currentword2" >
<br> <def>currentdef2</def><br> </entry><br> <entry word = "currentword3" ><br> <def>currentdef3</def><br> </entry><br></word_list><br><br>However, what happens is the next element always replaces the one before it and I end up with a .txt file that has only one element:
<br><br><word_list><br> <title name ="words" /><br> <entry word = "currentword3" ><br> <def>currentdef3</def><br> </entry><br></word_list><br><br>Here is my code:
<br><br> try {<br> BufferedWriter writer = new BufferedWriter (<br> new OutputStreamWriter (<br> new FileOutputStream ("
words.txt"),<br> "UTF-8"));<br> Format format = Format.getPrettyFormat();<br> format.setEncoding("UTF-8");<br> XMLOutputter outputter = new XMLOutputter(format);
<br> writer.write("<word_list>");<br> writer.newLine();<br> writer.write("<title name ='words' />");<br>
writer.newLine();<br> outputter.output(currentElement, writer);<br> writer.newLine();<br> writer.write("</word_list>");<br>
writer.flush();<br> writer.close();<br> }<br> catch (IOException io) {}<br><br>I'm not sure what to do and I'd appreciate your help. Thanks!