[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: XML Java Parser...
- From: Mike Brown <mike@skew.org>
- To: AMIT JAIN <amit3131@yahoo.com>
- Date: Wed, 28 Feb 2001 11:24:39 -0700 (MST)
AMIT JAIN wrote:
> We are doing a project in Java using Servlets and JSP.
> We got a requirement of uploading an XML file and
> parse it to get the data in java beans
If by uploading you mean you are using JSP to create an HTML form and
relying on a web browser to submit an XML document as part of form data
set, then you are going to find it is easy to do, but impossible to do
right, due to complications relating to character encoding that is poorly
spec'd and poorly implemented. Your document can get mangled by the sender
(the browser) and the receiver (the HTTP servlet/JSP implementation)
alike.
If you impose some usage requirements on your system, such as:
- only using certain browsers that are set to auto-detect the
encoding of the HTML documents they are processing (specifically,
those browsers that can be trusted to submit form data using the
same encoding as the HTML document containing the form)
- being aware of exactly how your servlet implementation goes about
decoding the form data when you access the data via the
getParameter methods, and making up for any discrepancies accordingly
(e.g., if you know getParameter will always assume iso-8859-1,
you can serialize the resulting String back to bytes and then read
them back in again as characters, this time using the encoding you
used for the original form)
...then you will be alright.
A better solution is to not use form data at all. Maybe use a form to tell
a separate servlet where to find the XML file, then have the servlet
incorporate the raw data from the file as the body of an HTTP request. On
the receiving end, use something like
<%
if ( request.getMethod().equals("POST") )
{
// get the request body
ServletInputStream requestbodybytes = request.getInputStream();
// make a parser
DOMParser parser = new DOMParser();
// register an error handler with the parser
// (you'll need to create a class that implements the 3 methods in
// org.xml.sax.ErrorHandler. I called mine SAXParseErrorHandler)
SAXParseErrorHandler errhandler = new SAXParseErrorHandler( out );
parser.setErrorHandler( errhandler );
// prepare an input source for the parser
InputSource parserinput = new InputSource( requestbodybytes );
// parse it
parser.parse( parserinput );
}
%>
This makes it difficult to use an HTML form to edit the XML, though, if
that's what you were hoping to do.
Good luck.
- Mike
____________________________________________________________________
Mike J. Brown, software engineer at My XML/XSL resources:
webb.net in Denver, Colorado, USA http://skew.org/xml/