Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xml-dev] practical question re: Java/XML handling

From: Mike Sokolov <sokolov@--------.--->
To: xml-dev@-----.---.---
Date: 9/3/2009 1:28:00 PM
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;
                }
            }
        }

_______________________________________________________________________

XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.

[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@l...
subscribe: xml-dev-subscribe@l...
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent