You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Brian Delahunty <bd...@tssg.org> on 2003/06/16 16:21:38 UTC
Custom Generators - SAX
Hi,
I'm not sure if this is where I should be asking this.
I'm writing a custom generator that needs to just return an XML document that's contained within a String. Now in the past I have used code like this [Simple Hello-World]:
public class SMSGenerator extends AbstractGenerator {
AttributesImpl emptyAttr = new AttributesImpl();
public void generate() throws SAXException
{
contentHandler.startDocument();
contentHandler.startElement("", "abc", "def", emptyAttr);
contentHandler.characters("Hello World".toCharArray(), 0, "Hello World".length());
contentHandler.endElement("","abc", "def");
contentHandler.endDocument();
}
}
I now have the XML file in a String and I need this to be "returned" by the generator. I was just wondering if anybody knew how to do that without going through the String, breaking it into "Elements" and "characters"
Thanks in advance
Brian
Re: Custom Generators - SAX
Posted by Holger Dewes <h....@insiders.de>.
Brian Delahunty wrote:
> Hi,
>
> I'm not sure if this is where I should be asking this.
>
> I'm writing a custom generator that needs to just return an XML document
> that's contained within a String. Now in the past I have used code like
> this [Simple Hello-World]:
> *
> public* *class* SMSGenerator *extends* AbstractGenerator {
> AttributesImpl emptyAttr = *new* AttributesImpl();
> * public* *void* generate() *throws* SAXException
> {
> contentHandler.startDocument();
> contentHandler.startElement("", "abc", "def", emptyAttr);
> contentHandler.characters("Hello World".toCharArray(), 0, "Hello
> World".length());
> contentHandler.endElement("","abc", "def");
> contentHandler.endDocument();
> }
> }
>
> I now have the XML file in a String and I need this to be "returned" by
> the generator. I was just wondering if anybody knew how to do that
> without going through the String, breaking it into "Elements" and
> "characters"
>
You use a SAX parser, like this:
public void generate() throws SAXException
{
// ...
org.xml.sax.XMLReader xr =
org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
xr.setContentHandler(this.contentHandler);
String result = ...;
xr.parse(new org.xml.sax.InputSource(result.getBytes());
}
This should do the trick. You might have to specify the character
encoding when converting the String to a byte array with getBytes().
--
Holger Dewes
insiders GmbH Tel. +49 6131 98210-0
Wissensbasierte Systeme Fax +49 6131 98210-11
Wilh.-Th.-Römheld-Str. 18 h.dewes@insiders.de
55130 Mainz http://www.insiders.de/
---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org