InterviewSolution
| 1. |
How Do I Turn On Validation? |
|
Answer» You can turn validation on and off via methods available on the SAX2 XMLFilter interface. While only the SAXParser implements the XMLFilterinterface, the methods required for turning on validation are available to both parser classes, DOM and SAX. The code snippet below shows how to turn validation on -- assume that parser is an INSTANCE of either org.apache.xerces.parsers.SAXParser ororg.apache.xerces.parsers.DOMParser. parser.setFeature("http://xml.org/sax/features/validation", true); IMPORTANT!Simply turning on validation will not make Xerces ACTUALLY report the errors that it detects. For this, you need to implement theorg.xml.sax.ErrorHandler interface and register your implementation with the parser using the setErrorHandler method. You can turn validation on and off via methods available on the SAX2 XMLFilter interface. While only the SAXParser implements the XMLFilterinterface, the methods required for turning on validation are available to both parser classes, DOM and SAX. The code snippet below shows how to turn validation on -- assume that parser is an instance of either org.apache.xerces.parsers.SAXParser ororg.apache.xerces.parsers.DOMParser. parser.setFeature("http://xml.org/sax/features/validation", true); IMPORTANT!Simply turning on validation will not make Xerces actually report the errors that it detects. For this, you need to implement theorg.xml.sax.ErrorHandler interface and register your implementation with the parser using the setErrorHandler method. |
|