LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java xml DOM question (http://www.linuxquestions.org/questions/programming-9/java-xml-dom-question-775573/)

kpachopoulos 12-14-2009 10:06 AM

java xml DOM question
 
Hi,
i have a program, that uses a DOM parser, to parse a set of XML files according to a specific DTD. This set includes XMLs that are valid to the DTD and XMLs, that are not valid. How can i filter out the invalid ones? I could read a DOCTYPE or some field of them, but first i must load them; that is not possible however, since they are not valid according to the DTD and produce exceptions first.

Thank you

indienick 12-15-2009 06:54 PM

If they are not valid to the DTD, does it throw an exception of any sort? If so, when you catch the exception, skip over that XML file and carry on to the next one.

kpachopoulos 12-16-2009 09:35 AM

Thanks indienick, this would work, but somehow i didn't like the design behind this and maybe catching exceptions would be insufficient. Another way would be:

Code:

        db.setEntityResolver(new EntityResolver() {
                                @Override
                                public InputSource resolveEntity(String publicId, String systemId)
                                                throws SAXException, IOException {                       
                            if (!systemId.contains("XMLDefinition.dtd")) {
                                return new InputSource(new StringReader(""));
                            } else {
                                return null;
                            }
                                }                               
                        });



All times are GMT -5. The time now is 11:59 AM.