Re: [javaCompiler-users] compiles and executes but throws java exception --help
Status: Beta
Brought to you by:
soapy
|
From: Marco T. <mt...@gm...> - 2007-02-14 13:45:02
|
Steven Blade wrote:
> <snip>
>
> public void trsToXml(String ofile)
> {
> SAXTransformerFactory
> factory=(SAXTransformerFactory)SAXTransformerFactory.newInstance();
> //the exception points to this line
> try{
> FileWriter fw=new FileWriter(ofile);
> PrintWriter pw=new PrintWriter(fw,true);
> StreamResult stream=new StreamResult(pw);
> handler=factory.newTransformerHandler();
> Transformer trans=handler.getTransformer();
> trans.setOutputProperty (OutputKeys.ENCODING,"ISO-8859-1");
> trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"trs_xml.dtd");
> trans.setOutputProperty(OutputKeys.INDENT,"yes");
> handler.setResult(stream);
> handler.startDocument();
> AttributesImpl atts=new AttributesImpl();
> handler.startElement("", "", "test", atts);
> atts.clear();
> handler.startElement("", "", "foo", atts);
> handler.characters(new char[]{'a','b','c'}, 0, 3);
> handler.endElement("", "", "foo");
> handler.endElement("", "", "test");
> handler.endDocument();
> pw.flush();
> pw.close();
> }catch(Exception _){_.printStackTrace();}
> this one throws the first exception
> }
> public void xmlToTrs(File sfile,String oFileName)
> {
> PrintWriter pWriter=null;
> try{
> SAXParserFactory factory=SAXParserFactory.newInstance(); // the
> exception points to this line
> factory.setValidating(true);
> SAXParser parser=factory.newSAXParser();
> boolean proceed=true;
> File trs=new File(oFileName);
> if(trs.isFile() && trs.exists())
> {
> System.out.println("the file \""+oFileName+"\" already exists\nDo
> you wish to overwrite it ? Y/N\n");
> BufferedReader input=new BufferedReader(new
> InputStreamReader(System.in <http://System.in>));
> proceed=input.readLine().equalsIgnoreCase("y");
> }
> if(proceed)
> {
> parser.parse (sfile, new DefaultHandler());
> pWriter=new PrintWriter(new FileWriter(trs),true);
> pWriter.write(sHeading.toString()+sGeneral.toString()+sItems.toString());
> pWriter.flush();
> }
> }
> catch(Exception _){_.printStackTrace();}
> finally{if(pWriter!=null) pWriter.close();}
> }
> and this one the second
Unfortunately I can't do much with them. What I meant was a
self-containing minimal example that I can run. E.g. some stupid parsing
or transforming. A XML HelloWorld if you like...
So, I just tested the:
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
And I need this to get it working:
static
{
gnu.xml.dom.ls.SAXEventSink.class.getName();
gnu.xml.transform.TransformerFactoryImpl.class.getName();
gnu.xml.xpath.XPathFactoryImpl.class.getName();
org.xml.sax.ContentHandler.class.getName();
}
Please read the very small "Step 1 of 4: Source" part in the manual to
understand why this is necessary. Maybe you need more of them for other
XML stuff too...
I'd still appreciate a minimal XML HelloWorld if you have time. Attached
and not inlined if possible :-)
Hope it helps...
Marco
|