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

Jeff Garza jeff.garza at gmail.com
Tue May 22 10:14:51 PDT 2007


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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20070522/51e7998d/attachment.htm


More information about the jdom-interest mailing list