Solved

writing xml tags to the file system as chunks using StreamResult

Posted on 2006-07-02
6
486 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
Comment Utility
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
Comment Utility
Typo:

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

Expert Comment

by:mukundha_expert
Comment Utility
try,

StringWriter out = new StringWriter () ;

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

XmlFileWriter xfw = new XmlFileWriter (out ) ;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:mukundha_expert
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

7 Experts available now in Live!

Get 1:1 Help Now