Re: Writing a list of currentElements using XMLOutputter

"Grzegorz Kaczor" <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
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.
Grzegorz
On 22/05/07, Jeff Garza <[email protected]> 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/[email protected]>

-- "Choć tyle wiemy własnym doświadczeniem:W nas jest Raj, Piekło - i do obu - szlaki."J.K.
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.