You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Gil Hauer <gi...@technolog.ca> on 2003/04/30 19:37:59 UTC
building a DOM from with a JSP page
Hello,
I'm trying to read an XML file and build a DOM from within a JSP page. I
have the code working as a standalone java application. After parsing
the XML file I print out the Document object and get this result:
document = org.apache.crimson.tree.XmlDocument@f9f9d8
When I put this same (well, the body is identical) code in a JSP page I
get this result:
document = [#document: null]
I've taken the code from Sun's XML tutorial (included below).
Has anyone experienced this behaviour? At first I thought that the file
was not being read so I changed the filename to one that is invalid and
got a Java stacktrace (as expected); this means that it's actually
readin the file.
I don't think I'm getting any exceptions since I 'catch' the following:
SAXParseException
SAXException
ParserConfigurationException
IOException
Finally, here's the code:
Document document;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(topics_filename);
System.err.println("document = " + document);
} catch (SAXParseException spe) {
// Error generated by the parser
System.out.println("\n** Parsing error"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage() );
// Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null) {
x = spe.getException();
}
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
SOLVED: Re: building a DOM from with a JSP page
Posted by Gil Hauer <gi...@technolog.ca>.
Well, it turns out the 'document' is not really null; I can get at the
nodes with getElementByTagName. The 'null' text in the printout is
simple there to confuse :)
Thanks,
Gil
On Wed, 2003-04-30 at 13:37, Gil Hauer wrote:
> Hello,
>
> I'm trying to read an XML file and build a DOM from within a JSP page. I
> have the code working as a standalone java application. After parsing
> the XML file I print out the Document object and get this result:
>
> document = org.apache.crimson.tree.XmlDocument@f9f9d8
>
> When I put this same (well, the body is identical) code in a JSP page I
> get this result:
>
>
> document = [#document: null]
>
> I've taken the code from Sun's XML tutorial (included below).
>
> Has anyone experienced this behaviour? At first I thought that the file
> was not being read so I changed the filename to one that is invalid and
> got a Java stacktrace (as expected); this means that it's actually
> readin the file.
>
> I don't think I'm getting any exceptions since I 'catch' the following:
>
> SAXParseException
> SAXException
> ParserConfigurationException
> IOException
>
> Finally, here's the code:
>
> Document document;
>
> DocumentBuilderFactory factory =
> DocumentBuilderFactory.newInstance();
> //factory.setValidating(true);
> //factory.setNamespaceAware(true);
> try {
> DocumentBuilder builder = factory.newDocumentBuilder();
> document = builder.parse(topics_filename);
> System.err.println("document = " + document);
> } catch (SAXParseException spe) {
> // Error generated by the parser
> System.out.println("\n** Parsing error"
> + ", line " + spe.getLineNumber()
> + ", uri " + spe.getSystemId());
> System.out.println(" " + spe.getMessage() );
>
> // Use the contained exception, if any
> Exception x = spe;
> if (spe.getException() != null) {
> x = spe.getException();
> }
> x.printStackTrace();
>
> } catch (SAXException sxe) {
> // Error generated during parsing)
> Exception x = sxe;
> if (sxe.getException() != null)
> x = sxe.getException();
> x.printStackTrace();
>
> } catch (ParserConfigurationException pce) {
> // Parser with specified options can't be built
> pce.printStackTrace();
>
> } catch (IOException ioe) {
> // I/O error
> ioe.printStackTrace();
> }
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org