Solved

writing xml tags to the file system as chunks using StreamResult

Posted on 2006-07-02
6
502 Views
Last Modified: 2013-11-23
Hi everybody.

I have to write an application that gets data constantly that should be structured as xml and saved/appened as xml file on the disk.
I have written a simple class that parses the  data to generate xml tags , I am totally stick to SAX and I cant use dom or others except when it is memory friendly since it is very critical in my case(this is for those who might recommend me dom or equivalent) . in my code I have this
 transformer.transform(source, result);
 which as seen takes the source and writes to the result which is :
StreamResult result = new StreamResult(System.out);

Now I would like to have my output written to a file on the disk instead of console. I have written for this purpose the following class for performance purposes( it just helps to reduce the flushing by getting the tags stored till a certain size and then write it )
 The question is since the StereamResult accepts only writer or outputstream, then how can I use my class above to initialse the result object and how to use the functions of the class ?
The class is

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

public class XmlFileWriter {

      

          private PrintWriter out = null ;
        
          public static final int buffer_size = 5000*20 ;
          private StringBuffer buffer = new StringBuffer( ) ;  
        
          public  XmlFileWriter( OutputStream out )
          {
              this.out = new PrintWriter( out ) ;
          }
        
          public void write( String str ) throws IOException
          {
              buffer.append( str+"\r\n" ) ;
              flush() ;
          }
        
          public boolean flush() throws IOException
          {
              if ( buffer.capacity() >= buffer_size )
              {
                  out.println( buffer.toString() ) ;
                  buffer.setLength( 0 ) ;
                  out.flush() ;
                  return true ;
              }
              |return false ;
          }
        
          public void close()
          {
              try
              {
                  if ( buffer.capacity() > 0 )
                  {
                      out.println( buffer.toString() ) ;
                      out.flush() ;
                      buffer.setLength( 0 ) ;
                  }
                  out.close() ;
                  out = null ;
              } catch (Exception e) {}
          
        
      }

      

          

}


to make it shorter, the transformer flushes and writes directly each time the event of element.start or element.end fires, I would like to control it and let it only write to the file when I need to by calling the function called write above, I would like to accumulate the tags and then flush them to a disk explicitly.


0
Comment
Question by:alifaik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026299
Not 100% of what you need but you could try

OutputStream out = new FileOutputStream("result.xml");
StreamResult sr = new StreamResult(out);
XmlFileWriter xfw = XmlFileWriter(out);
//close 'out'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026300
Typo:

Not 100% of what you need=Not 100% sure of what you need
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17028663
try,

StringWriter out = new StringWriter () ;

transfomer.transform ( source, new StreamResult ( out)  ) ;

XmlFileWriter xfw = new XmlFileWriter (out ) ;
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17028666
sorry,

StringWriter out = new StringWriter () ;
transfomer.transform ( source, new StreamResult ( out)  ) ;

FileOutputStream file = new FileOutPutStream ( file ) ;
XmlFileWriter xfw = new XmlFileWriter ( file ) ;

xfw.write ( out.toString () ) ;
0
 
LVL 1

Author Comment

by:alifaik
ID: 17035995
Hi everybody,

Sorry for not answering since I was not in my city, thx mukundha_expert , your comment has solved the problem, I have one more question, how can I know about the various objects that I might need, for example I knew that I needed some stream or string to write the tags temporarily so that I can send them as arguments later on, but I didnt know iit is called StringWriter , how can I reach this in the future? can you recommend me a help tutorials please
0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 500 total points
ID: 17036154
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question