  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Writing a list of currentElements using XMLOutputter | Writing a list of currentElements using XMLOutputter 2007-05-22 - By Jeff Garza
Back 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 (See http://UTF-8.ora-code.com)")); Format format = Format.getPrettyFormat(); format.setEncoding("UTF-8 (See http://UTF-8.ora-code.com)"); 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!
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: <br><br><word_list><br> <title name ="words" /> ;<br> <entry word = "currentword1" ><br> <def>currentdef1</def><br> < /entry><br> <entry word = "currentword2" > <br> <def>currentdef2</def><br> </entry><br> <entry word = "currentword3 " ><br> <def>currentdef3< /def><br> </entry><br></word_list><br><br>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: <br><br><word_list><br> <title name ="words" /> ;<br> <entry word = "currentword3" ><br> <def>currentdef3</def><br> < /entry><br></word_list><br><br>Here is my code: <br><br>   ; try {<br>   ;BufferedWriter writer = new BufferedWriter (<br> new OutputStreamWriter (<br> new FileOutputStream (" words.txt"),<br> "UTF-8 (See http://UTF-8.ora-code.com)"));<br> Format format = Format.getPrettyFormat();<br>  ; format .setEncoding("UTF-8 (See http://UTF-8.ora-code.com)");<br> XMLOutputter outputter = new XMLOutputter (format); <br> writer.write("<word_list>");<br> writer.newLine();<br>  ; writer .write("<title name ='words' />");<br> writer.newLine();<br> outputter.output(currentElement, writer);<br>   ; writer.newLine();<br> writer.write("</word _list>");<br>   ; writer.flush();<br> writer.close();<br> }<br> catch (IOException io) {}<br><br>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@(protected)
|
|
 |