xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jayne_Peac...@jba.co.uk
Subject RE: Required - example java code to demonstrate error reporting
Date Fri, 07 Apr 2000 10:46:53 GMT
Thank you - I have tried you suggestion and here a sample from my code :

public class testSchema {
static DOMParser parser = new DOMParser();  // same result with DOM or SAX
public static void main (String args[]) {
try {
String xmlFile = args[0];
       MyErrorHandler myErrorHandler = new MyErrorHandler();
       parser.setErrorHandler(myErrorHandler);
       parser.setFeature( "http://xml.org/sax/features/validation", true);
       parser.setValidating(true);
       parser.parse(xmlFile);
       Document document = parser.getDocument();
   } catch (SAXException se) {
     System.out.println("testSchema - in SAXException");
       se.printStackTrace();
   } catch (IOException ioe) {
     System.out.println("testSchema - in IOException");
       ioe.printStackTrace();
   } catch (Exception e) {();ma
     System.out.println("testSchema - in Exception"+ e.toString());
   }
 }
}

class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException ex) {
        System.err.println("[test Warning] "+
                           getLocationString(ex)+": "+
                           ex.getMessage());
                  return;
    }
public void error(SAXParseException ex) {
        System.err.println("[test Error] "+
                           getLocationString(ex)+": "+
                           ex.getMessage());
                  return;
    }

    /** Fatal error. */
    public void fatalError(SAXParseException ex) throws SAXException {
        System.err.println("[test Fatal Error] "+
                           getLocationString(ex)+": "+
                           ex.getMessage());
                  return;
//        throw ex;
    }

    /** Returns a string of the location. */
    private String getLocationString(SAXParseException ex) {
        StringBuffer str = new StringBuffer();
        String systemId = ex.getSystemId();
        if (systemId != null) {
            int index = systemId.lastIndexOf('/');
            if (index != -1)
                systemId = systemId.substring(index + 1);
            str.append(systemId);
        }
        str.append(':');
        str.append(ex.getLineNumber());
        str.append(':');
        str.append(ex.getColumnNumber());
        return str.toString();
    } // getLocationString(SAXParseException):String
}


I use the example xml and schemas from XML4J_3_0_1\data\personal-schema.xml
and personal.xsd, if for example I change the minOccurs of person to 9 and
maxOccurs to 9,
and the the personal-schema contains only six person elements - I would
expect and error exception to be thrown - but it is not - any ideas ?



Mime
View raw message