Solved

writing xml tags to the file system as chunks using StreamResult

Posted on 2006-07-02
6
499 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 112
jmss example java 2 48
maximize the sum of fractions 33 74
Java pass by reference 3 74
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

732 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