Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

XML + XSD: Is it possible to get all errors against the XSD?

Thread view: 
Markus - 22 Nov 2005 13:06 GMT
Currenty I'm parsing a XML in this way:
...
try {
 m_builder = new SAXBuilder(a_b_validate);

m_builder.setFeature("http://apache.org/xml/features/validation/schema",
a_b_validate);
 m_document = m_builder.build(a_inputStreamXML);
 m_builder.setErrorHandler(m_SAXErrorHandler);
 m_rootElement = m_document.getRootElement();
catch (JDOMException e){
 System.out.println(e.getMessage());
} ...
If a_b_validate = false there is no Exception if the Document is
wellformed.
If a_b_validate = true and there are errors in the file I get the first
occured error in e.

Is there a posibillity to get all errors against the XSD at the first
parse?

Greetings

Markus
Markus - 22 Nov 2005 14:53 GMT
Here is the solution: :-)

m_SAXErrorHandler = new ErrorHandlerImpl();
m_builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
m_builder.setValidation(a_b_validate);
m_builder.setErrorHandler(m_SAXErrorHandler);
m_builder.setFeature("http://apache.org/xml/features/validation/schema",
a_b_validate);
m_document = m_builder.build(a_inputStreamXML);
m_rootElement = m_document.getRootElement();

If the document is not wellformed a JDOMException is throws and if
errors against the XSD exist they a stored in m_SAXErrorHandler (a
class which implements ErrorHandler).

Example for ErrorHandlerImpl:
public class SAXErrorHandler implements ErrorHandler {

       ArrayList m_warnings = new ArrayList();
       ArrayList m_errors = new ArrayList();
       ArrayList m_fatalErrors = new ArrayList();

       /**
        * @see org.xml.sax.ErrorHandler#warning(SAXParseException)
        */
       public void warning(SAXParseException arg0) throws SAXException
{
               m_warnings.add(arg0);
               System.out.println("Warning:   " +
arg0.getLocalizedMessage());
       }

       /**
        * @see org.xml.sax.ErrorHandler#error(SAXParseException)
        */
       public void error(SAXParseException arg0) throws SAXException {
               m_errors.add(arg0);
               System.out.println("Error:     " +
arg0.getLocalizedMessage());
       }

       /**
        * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
        */
       public void fatalError(SAXParseException arg0) throws
SAXException {
               m_fatalErrors.add(arg0);
               System.out.println("FatalError: " +
arg0.getLocalizedMessage());
       }

       public boolean hasWarnings() {
               return !this.m_warnings.isEmpty();
       }

       public boolean hasErrors() {
               return !this.m_errors.isEmpty();
       }

       public boolean hasFatalErrors() {
               return !this.m_fatalErrors.isEmpty();
       }

       public ArrayList getWarnings() {
               return this.m_warnings;
       }

       public ArrayList getErrors() {
               return this.m_errors;
       }

       public ArrayList getFatalErrors() {
               return this.m_fatalErrors;
       }

}

Greetings

Markus


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.