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

practical question re: Java/XML handling

  • From: Mike Sokolov <sokolov@ifactory.com>
  • To: xml-dev@lists.xml.org
  • Date: Thu, 03 Sep 2009 09:26:19 -0400

practical question re: Java/XML handling
After all the discussion about "What is data?" I don't know if this list 
is the place to discuss actual details of implementation, but please 
feel free to send me elsewhere if you can think of a better venue.

I have a need to handle XML that references a non-existent DTD.  The DTD 
is irrelevant to the actual processing of the XML, and isn't available 
anywhere, but it is declared in in the DOCTYPE.  I'm sure many of you 
have encountered this situation: it's practically the norm, in my 
experience.

After years of dealing with this inherently unsatisfactory situation in 
a variety of ways, I came up with a new one that I am liking at the 
moment, which is to insert a Stream into a Java XML processing stack 
that strips out the prolog of the XML document before handing it off to 
a parser.  This has the nice property that it doesn't require 
modifications to the stored XML files.  It loses PIs and comments and 
the XML decl, but I can live with that.

My question is twofold:

1) does the following code snippet actually do what it is claiming to?  
Does anybody see any obvious mistakes?  My knowledge of the format of 
DOCTYPE decls and so on is somewhat limited.  I read the spec and this 
seems to work on the examples I have, but I suspect there are some cases 
I'm not handling.

2) Is there a better approach?  Existing code to do the same thing?  
Some way to tell parsers to ignore the DOCTYPE (even though that seems 
to run counter to the spec)?

Thanks for your attention...

-Mike Sokolov

    /**
     * An InputStream for XML that strips off the prolog of an XML
     * document.  The idea is to avoid having to prevent parsers from 
attempting
     * to process an external DTD.
     *
     * @author sokolov
     *
     */
    class XmlNoPrologInputStream extends PushbackInputStream {
        
        XmlNoPrologInputStream (InputStream base) throws IOException {
            super (base, 2);
            int c;
            while ((c = read()) >= 0) {
                if (c == '<') {
                    int c1 = read();
                    if (c1 < 0) {
                        // ill-formed
                        reset();
                        return;
                    }
                    // XML declaration, PI, comment or DOCTYPE
                    if (c1 == '?' || c1 == '!')
                        continue;
                    // must be the start of the document: arrange to begin
                    // reading here
                    unread(c1);
                    unread(c);
                    return;
                }
            }
        }


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


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.