[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML Java Parser...

  • From: Mike Brown <mike@s...>
  • To: AMIT JAIN <amit3131@y...>
  • Date: Wed, 28 Feb 2001 11:24:39 -0700 (MST)

java parser.parse
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/

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.