Re: Catching XMLSerializer O/P

2001-10-22 Thread Shailendra Vasale


hi Sébastien,


   Thanks for your reply.

   What I'm trying to do is - I want to save the result 
which is getting displayed on the browser in a file.
   So I'm catching the events in XMLSerializer,when I catch the characters event I get 
the whole document content inside each element.What I understand is - the 
TraxTansformer inputs a ResultSet tree of XML doc to XMLSerializer.

   Here is the sample code of XML,XSL and output file --

XML.xml 
-


Hello

This is my first Cocoon2 b02 page! 



-

XSL.xsl
-

http://www.w3.org/1999/XSL/Transform";>






  
   

   
  

  
   
 

   


-

The saved file should be -

xmlFile.xml
-


Hello
This is my first Cocoon2 b02 page! 



-

The XMLSerializer for this is -

-
import org.apache.avalon.excalibur.pool.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import org.apache.cocoon.serialization.*;
import org.xml.sax.*;
import org.apache.cocoon.components.store.FilesystemStore;
import java.util.Vector;
import java.util.StringTokenizer;
import org.apache.avalon.framework.logger.Loggable;
import javax.xml.transform.sax.SAXResult;
import org.apache.cocoon.xml.XMLConsumer;
import com.sun.xml.tree.*;

public class XMLSerializer1 extends AbstractTextSerializer implements 
Serializable,Poolable,XMLConsumer{

private TransformerHandler handler;
private FileWriter xmlFile;

public XMLSerializer1() {
}

  public void setOutputStream(OutputStream out) {
 try {
XmlDocument xmlDoc = new XmlDocument();
super.setOutputStream(out);
this.handler = getTransformerFactory
().newTransformerHandler();
format.put(OutputKeys.METHOD,"xml");
handler.setResult(new StreamResult(out));
handler.getTransformer().setOutputProperties
(format);
super.setContentHandler(handler);
super.setLexicalHandler(handler);
this.setContentHandler(handler);
this.setLexicalHandler(handler);
} catch (Exception e)
{
System.out.println("Error in XMLSerializer1 : "+e);
   throw new RuntimeException(e.toString());
}
}
 
  public void characters(char[] ch, int start, int length) throws SAXException {
this.contentHandler.characters(ch, start, length);
 Writing element value in final xmlFile
try{
String value = new String(ch);
char buffer[] = new char[value.length()];
value.getChars(0,value.length(),buffer,0);
xmlFile.write(buffer);
}catch(Exception e)
{
System.out.println("Error : "+e);
}
  }

 public void endDocument() throws SAXException {
   this.contentHandler.endDocument();
try
{
xmlFile.close();
}catch(Exception e)
{
System.out.println("Error : "+e);
}

  }

   public void endElement(String namespaceURI, String localName, String rawName)
throws SAXException
  {
  /** Writing endElement in final xmlFile**/
  try{
String endTag = "";
char buffer[] = new char[endTag.length()];
endTag.getChars(0,endTag.length(),buffer,0);
xmlFile.write(buffer);
}catch(Exception e)
{
System.out.println("Error : "+e);
}
this.contentHandler.endElement(namespaceURI, localName, rawName);
  }

   public void startDocument() throws SAXException {
this.contentHandler.startDocument();
try
{
xmlFile = new FileWriter("xmlFile.xml",true);
String pi = "";
char buffer[] = new char[pi.length()];
pi.getChars(0,pi.length(),buffer,0);
xmlFile.write(buffer);
}catch(Exception e)
{
System.out.println("Error : "+e);
}
  }

/**
  Receive notification of ignorable whitespace in element content.
*/ 
  public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException

Re: Catching XMLSerializer O/P

2001-10-18 Thread Sebastien Koechlin

shailendra vasale wrote:

> I'm trying to catch the events in XMLSerializer(which is getting displayed on 
>browser)
> but what I get is - When I catch the startElement and endElement events I get the 
>elements
> as specified in XSL.
> But when I catch the characters event I get the entire content of XML in each element
> instead of getting the respecive element content.
> 
> Can anyone pls help me out? How can catch these events which is displayed on the
> browser either in Serializer or Transformer.

Are you using start and len in public void characters(char c[], int
start, int len)?
Can you send a 5-15 lines code sample where you get the entire content
of your XML
document?

-- 
Sébastien Koechlin - IVision - [EMAIL PROTECTED]

-
Please check that your question has not already been answered in the
FAQ before posting. 

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail: <[EMAIL PROTECTED]>