[jdom-interest] Writing a list of currentElements using XMLOutputter

Grzegorz Kaczor grzegorz.kaczor at gmail.com
Tue May 22 14:01:46 PDT 2007

Hi Jeff,
but in the code you create a new file for each currentElement so it isbehaving correctly.
You can write:(...)writer.newLine();writer.write("<title name ='words' />");writer.newLine();for (...) {   ... wait for user click ...   outputter.output...}writer.write("</word_list>");
Perhaps you will have to flush the writer too if you want to see theresults immediately after each iteration.
However if you have to have a correct XML file after each loopiteration I am afraid you will have to parse, modify and output theXML file again after each click. XML is not designed to support"appending" to the end. In such a situation I would suggest to createa simple text file, append to it and convert it to an XML documentafterwards.
On 22/05/07, Jeff Garza <jeff.garza at gmail.com> wrote:> 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:>> <word_list>>   <title name ="words" />>   <entry word = "currentword1" >>       <def>currentdef1</def>>   </entry>>   <entry word = "currentword2" >>       <def>currentdef2</def>>   </entry>>   <entry word = "currentword3" >>       <def>currentdef3</def>>   </entry>> </word_list>>> 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:>> <word_list>>   <title name ="words" />>   <entry word = "currentword3" >>       <def>currentdef3</def>>   </entry>> </word_list>>> Here is my code:>>                        try {>                           BufferedWriter writer = new> BufferedWriter (>                                    new OutputStreamWriter (>                                        new FileOutputStream> (" words.txt"),>           "UTF-8"));>                           Format format => Format.getPrettyFormat();>                           format.setEncoding("UTF-8");>                           XMLOutputter outputter = new> XMLOutputter(format);>                           writer.write("<word_list>");>                           writer.newLine();>                           writer.write("<title name ='words' />");>                            writer.newLine();>                           outputter.output(currentElement, writer);>        writer.newLine();>                           writer.write("</word_list>");>                            writer.flush();>                           writer.close();>                       }>                       catch (IOException io) {}>> I'm not sure what to do and I'd appreciate your help. Thanks!> _______________________________________________> To control your jdom-interest membership:> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com>

-- "Choć tyle wiemy własnym doświadczeniem:W nas jest Raj, Piekło - i do obu - szlaki."J.K.

More information about the jdom-interest mailing list