Solved

writing xml tags to the file system as chunks using StreamResult

Posted on 2006-07-02
6
491 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 36
micro services vs rest web services 16 84
Image decoding from Camera 3 72
Arrays.asList  VS  ArrayList 4 44
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now