Java Mailing List Archive

http://www.junlu.com/

Google
Google
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
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Writing a list of currentElements using XMLOutputter

Writing a list of currentElements using XMLOutputter

2007-05-22       - By Jeff Garza

 Back
Reply:     1     2     3     4     5     6  

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&#39;m trying to write a list of elements to a .txt file using XMLOutputter
and I&#39;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>&lt;word_list&gt;<br>&nbsp;&nbsp;&lt;title name =&quot;words&quot; /&gt
;<br>&nbsp;&nbsp;&lt;entry word = &quot;currentword1&quot; &gt;<br>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;def&gt;currentdef1&lt;/def&gt;<br>&nbsp;&nbsp;&lt;
/entry&gt;<br>&nbsp;&nbsp;&lt;entry word = &quot;currentword2&quot; &gt;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;def&gt;currentdef2&lt;/def&gt;<br>
&nbsp;&nbsp;&lt;/entry&gt;<br>&nbsp;&nbsp;&lt;entry word = &quot;currentword3
&quot; &gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;def&gt;currentdef3&lt;
/def&gt;<br>&nbsp;&nbsp;&lt;/entry&gt;<br>&lt;/word_list&gt;<br><br>However,
what happens is the next element always replaces the one before it and I end up
with&nbsp;a .txt file that has only one element:
<br><br>&lt;word_list&gt;<br>&nbsp;&nbsp;&lt;title name =&quot;words&quot; /&gt
;<br>&nbsp;&nbsp;&lt;entry word = &quot;currentword3&quot; &gt;<br>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;def&gt;currentdef3&lt;/def&gt;<br>&nbsp;&nbsp;&lt;
/entry&gt;<br>&lt;/word_list&gt;<br><br>Here is my code:
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;BufferedWriter writer = new BufferedWriter (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; new OutputStreamWriter (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new FileOutputStream (&quot;
words.txt&quot;),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&quot;UTF-8 (See http://UTF-8.ora-code.com)&quot;));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Format format = Format.getPrettyFormat();<br>&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format
.setEncoding(&quot;UTF-8 (See http://UTF-8.ora-code.com)&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XMLOutputter outputter = new XMLOutputter
(format);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;writer.write(&quot;&lt;word_list&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.newLine();<br>&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer
.write(&quot;&lt;title name =&#39;words&#39; /&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
writer.newLine();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;outputter.output(currentElement, writer);<br>&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp; writer.newLine();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(&quot;&lt;/word
_list&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;
writer.flush();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;writer.close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch
(IOException io) {}<br><br>I&#39;m not sure what to do and I&#39;d appreciate
your help. Thanks!

__ ____ ____ ____ ____ ____ ____ ____ ____ ____
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.