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

Grzegorz Kaczor grzegorz.kaczor at gmail.com
Wed May 23 15:03:36 PDT 2007


Hi Jeff,
below some notices:
1. you iterate over listIterator but the outputter outputscurrentElement and not listIterator.next() - so you output the sameelement multiple times
2. please note that advance() method always sets currentElement to thefirst element in wordList :). I am not sure if it is intended.
3. what is more, you can safely throw away the isWanted variable as itchanges nothing in the code you presented above.
4. you never call writer.close() so the writer cannot be closed. Thecode is unclear to me, I don't get the idea behind but this does notlook good.
I can take a look at it if you sent me the whole code (but to privateaddress, there is no point in spamming the discussion list). I am surethat the problem is not in JDOM but in improper usage of it.
Regards,Grzegorz
On 23/05/07, Jeff Garza <jeff.garza at gmail.com> wrote:> Whoops! I made some mistakes copying the code so please discard the previous> message and instead check this one:>>     List <Element> wordList;>     ListIterator listIterator;>     Element currentElement;>     boolean isWanted = false;>>         Btn.addActionListener(new ActionListener() {>             public void actionPerformed(ActionEvent e) {>                 try {>                     isWanted = true;>                         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(isWanted == true){>                                 while (listIterator.hasNext()){>> outputter.output(currentElement, writer);>                                     writer.newLine();>                                 }>                             }>                             else {>                             writer.write("</word_list>");>                             writer.flush();>                             writer.close();>                             }>                         }>                         catch (IOException io) {}>                   advance();>         }>         catch (NullPointerException ne) { }>>         }>     });>>     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();>                 }>             }>

-- "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