Results 1 to 2 of 2
  1. #1
    SharkDentist is offline Member
    Join Date
    Dec 2011
    Location
    Memphis, TN
    Posts
    6
    Rep Power
    0

    Default Why does Java parser require DTD when validating with a schema?

    I have a network monitoring program called JMonitor which must parse a configuration file. Inside the Configuration class is a method parseIOStream(InputStream). The configuration file can be on the user's PC, a local file or a URL. All options eventually use this method. After researching some examples on the web I coded it as shown below. errorHandler is a private class that implements the required interface. The program is being deployed using Java WebStart. I am using NetBeans 7.0.1 on Fedora Core 14 desktop with 1.6u29.

    The problem I encountered is that unless the XML file being parsed has a valid DTD declaration and a real DTD file, the error() method of the errorHandler gets bombarded with errors that cannot easily be distinguished from schema generated errors.

    The top tag of the document is <root>.

    For now I created a DTD, but it requires effort for something I don't really need. With a valid DTD, it works OK. I get an error when the schema detects one.

    Is there a way to tell the parser to ignore a DTD declaration or eliminated the DTD declaration entirely?

    If I use a SchemaFactory to validate, input stream of the target document gets closed before I can use the DocumentBuilder to parse it.

    If I don't use setValidating(true), the schema is ignored.

    Java Code:
      
            private MyErrorHandler errorHandler = new MyErrorHandler();  
    
            private boolean parseIOStream(InputStream ios) {
            
            Document dom;
            
            // First create factories for schema and DOM parse
            
            ClassLoader loader = Configuration.class.getClassLoader();
            StreamSource schemaSource = new StreamSource(loader.getResourceAsStream("com/fedex/eosquadrant/monitor/JMonitor.xsd"));
            SchemaFactory schf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
            // Next, configure parser to validate and then parse
            // Next, retrieve node list 
            
            try {
                Schema schema = schf.newSchema(schemaSource);
                dbf.setSchema(schema);
                dbf.setValidating(true);
                DocumentBuilder db = dbf.newDocumentBuilder();
                db.setErrorHandler(errorHandler);
                dom = db.parse(ios);
            } catch (ParserConfigurationException pce){
                System.out.println("DOM ParserConfigurationException");
                System.out.println(pce.getMessage());
                return(false);
            } catch (SAXException se) {
                System.out.println("DOM Parse SAXException");
                System.out.println(se.getMessage());
                return(false);
            } catch (IOException ioe) {
                System.out.println("DOM IOException");
                System.out.println(ioe.getMessage());
                return(false);
            }
            // If the config file parse detected any severe errors, just return
            if (errorHandler.getError()) return (false);
    etc.


    Java Code:
        private class MyErrorHandler implements ErrorHandler {
            public boolean severeError = false;
            @Override
            public void warning (SAXParseException spe) throws SAXException {
                System.out.println("SAX Parse Warning" + spe.getMessage());
            }
            @Override
            public void error(SAXParseException spe) throws SAXException {
                severeError = true;
                System.out.println("SAX Parse Error" + spe.getMessage());
            }
            @Override
            public void fatalError(SAXParseException spe) throws SAXException {
                severeError = true;
                System.out.println("SAX Parse FatalError" + spe.getMessage());
            }
            public boolean getError() {
                return severeError;
            }
        }
    Thanks in advance.

  2. #2
    SharkDentist is offline Member
    Join Date
    Dec 2011
    Location
    Memphis, TN
    Posts
    6
    Rep Power
    0

    Default Re: Why does Java parser require DTD when validating with a schema?

    I think I found the answer to this questions: http://people.apache.org/~edwingo/jaxp-faq.html


    The standard JAXP implementation requires a DTD in the document if you are going to validate it. It appears there is a way for you to actually manipulate the DTD in the document before you validate it, but it seems like a lot of work. I found an XML parser from Oracle where you can set the validation mode to "schema only".

    Those are the choices.

Similar Threads

  1. Validating XSD Schema in a web-service
    By sehudson in forum XML
    Replies: 0
    Last Post: 03-26-2011, 02:14 AM
  2. help on java mozilla html parser.....
    By nijil in forum New To Java
    Replies: 7
    Last Post: 03-01-2010, 06:49 PM
  3. DOM Parser for xml file in java
    By Ms.Ranjan in forum XML
    Replies: 2
    Last Post: 09-18-2009, 04:21 PM
  4. Developing HTML Parser in JAVA
    By shinojkk in forum Advanced Java
    Replies: 1
    Last Post: 01-18-2008, 08:07 PM
  5. Java Mozilla Html Parser 0.1.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-30-2007, 04:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •