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
---------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<page>
<title>Hello</title>
<content>
<paragraph>This is my first Cocoon2 b02 page!
</paragraph>
</content>
</page>
---------------------------------------------------------
XSL.xsl
---------------------------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="page">
<hellopage>
<xsl:apply-templates/>
</hellopage>
</xsl:template>
<xsl:template match="title">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="paragraph">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
</xsl:stylesheet>
---------------------------------------------------------
The saved file should be -
xmlFile.xml
---------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<hellopage>
<title>Hello</title>
<para>This is my first Cocoon2 b02 page!
</para>
</hellopage>
---------------------------------------------------------
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 = "</"+rawName+">";
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 = "<?xml version='1.0' encoding='ISO-8859-1'?>";
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
{
this.contentHandler.ignorableWhitespace(ch, start, length);
}
public void startElement(
String namespaceURI, String localName, String rawName, Attributes atts)
throws SAXException
{
/** Writing startElement in final xmlFile**/
try
{
String startTag = "<"+rawName+">";
char buffer[] = new char[startTag.length()];
startTag.getChars(0,startTag.length(),buffer,0);
xmlFile.write(buffer);
}catch(Exception e)
{
System.out.println("Error1 : "+e);
}
this.contentHandler.startElement(namespaceURI, localName, rawName, atts);
}
/**
* Recycle the serializer. GC instance variables
*/
public void recycle() {
super.recycle();
this.handler = null;
}
}
---------------------------------------------------------
I'm getting the xmlFile.xml somewhat like -
xmlFile.xml
---------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<hellopage>Hello
This is my first Cocoon2 b02 page!
<title>Hello
This is my first Cocoon2 b02 page!
</title>
<para>Hello
This is my first Cocoon2 b02 page!
</para>
</hellopage>
---------------------------------------------------------
Hope this helps to understand my problem.
Thanks.
Regards,
Shailendra Vasale
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. <http://xml.apache.org/cocoon/faqs.html>
To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail: <[EMAIL PROTECTED]>