Link to home
Start Free TrialLog in
Avatar of alifaik
alifaik

asked on

writing xml tags to the file system as chunks using StreamResult

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.


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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'
Typo:

Not 100% of what you need=Not 100% sure of what you need
Avatar of mukundha_expert
mukundha_expert

try,

StringWriter out = new StringWriter () ;

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

XmlFileWriter xfw = new XmlFileWriter (out ) ;
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 () ) ;
Avatar of alifaik

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mukundha_expert
mukundha_expert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial