Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

Jeff Garza

2007-05-23

Replies:

  Here is my latest attempt:

    List <Element> wordList;
    ListIterator listIterator;
    Element currentElement;
    boolean isWanted = false;

        Btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    isWrong = true;
                    if(isPrevious == false){
                        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 ();
                            if(isWrong == true){
                                while (listIterator.hasNext()){
                                    outputter.output(currentElement, writer);
                                    writer.newLine();
                                }
                            }
                            else {
                            writer.write("</word_list>");
                            writer.flush();
                            writer.close();
                            }
                        }
                        catch (IOException io) {}
                  advance();
         }

    public void advance() {
        isWanted= false;
            //begin, get and print element
            if(listIterator == null) {
                listIterator = wordList.listIterator();
                if( listIterator.hasNext()) {
                    currentElement = (Element)listIterator.next();
                    txtWord.setText("<html>"+currentElement.getAttribute("word").getValue());
                }
            }
                //reached end, start over
                else {
                    listIterator = wordList.listIterator();
                    currentElement = (Element)listIterator.next();
                }
            }

This creates the words.txt file like it should but it locks the Btn and won't close the stream so I end up with a neverending document.

This is killing me.... but I don't want to give up because I've spent way too much time trying to figure it out!

On 5/23/07, Grzegorz Kaczor <grzegorz.kaczor@gmail.com> wrote:
please attach the whole loop code that sets currentElement too. I
think that the code you posted is too little to say something about
the reason of the problem.

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.