xml-xalan-j-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gordon Ross" <G.R...@ccw.gov.uk>
Subject Custom SAX Parser
Date Tue, 13 May 2003 12:49:50 GMT
Hi,

I'm trying to use a custom SAX parser with Xalan. I started by using
the code below to use the standard SAX parser, and this works great:

    javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();
    Templates template = tFactory.newTemplates(new
StreamSource("servers.xslt"));

    javax.xml.transform.sax.TransformerHandler saxTransformHandler =
((SAXTransformerFactory)tFactory).newTransformerHandler(template);
    saxTransformHandler.setResult(new StreamResult(System.out));

    XMLReader reader =
org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
  reader.setContentHandler(saxTransformHandler);
    reader.parse("severlist.xml");

I then wanted to use a custom SAX parser (which actually generates SAX
events avoiding writting XML and then parsing that XML)

I set this up using this:

    javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();
    Templates template = tFactory.newTemplates(new
StreamSource("servers.xslt"));

    javax.xml.transform.sax.TransformerHandler saxTransformHandler =
((SAXTransformerFactory)tFactory).newTransformerHandler(template);
    saxTransformHandler.setResult(new StreamResult(System.out));

    fakeXML(saxTransformHandler);

where fakeXML fires SAX events. A cut-down version of it is:

  public static void fakeXML(ContentHandler ch) throws SAXException
  {
    char charray[];
    String myString;

    ch.startDocument();
    ch.startElement("", "", "serverlist", EMPTY_ATTR);
    ch.startElement("","","serverTypes",EMPTY_ATTR);
    charray = (new String("Master")).toCharArray();
    myString = "Master";
    ch.characters(myString.toCharArray(),0, myString.length());
    ch.endElement("","","serverTypes");

    ch.startElement("","","server",EMPTY_ATTR);
      ch.startElement("","","name",EMPTY_ATTR);
        charray = (new String("ISIS32")).toCharArray();
        ch.characters(charray,0,charray.length);
      ch.endElement("","","serverTypes");
      ch.startElement("","","description",EMPTY_ATTR);
        charray = (new String("Some Junk")).toCharArray();
        ch.characters(charray,0, charray.length);
      ch.endElement("","","description");
      ch.startElement("","","state",EMPTY_ATTR);
        charray = (new String("Sleeping")).toCharArray();
        ch.characters(charray,0,charray.length);
      ch.endElement("","","description");
      ch.startElement("","","info",EMPTY_ATTR);
        charray = (new String("home/vol2/test")).toCharArray();
        ch.characters(charray,0,charray.length);
      ch.endElement("","","info");
    ch.endElement("","","server");

    ch.endElement("","","serverlist");
    ch.endDocument();
  }

This code "generates" the XML that worked with the first example.

However, when I try this, the output from Xalan doesn't have anything
from my generated SAX/XML.

Can anyone point me in the right direction ?

Thanks,

GTG


Gordon Ross,
Network Manager/Rheolwr Rhydwaith
Countryside Council for Wales/Cyngor Cefn Gwlad Cymru

Mime
View raw message