Advertisement
Guest User

sax

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. public static boolean validate(String xml)throws ParserConfigurationException, IOException, org.xml.sax.SAXException{
  2.  
  3. try {
  4.  
  5. SAXParserFactory factory = SAXParserFactory.newInstance();
  6. factory.setValidating(true);
  7. factory.setNamespaceAware(true);
  8.  
  9. SAXParser parser = factory.newSAXParser();
  10.  
  11. XMLReader reader = parser.getXMLReader();
  12. reader.setErrorHandler(
  13. new ErrorHandler() {
  14. @Override
  15. public void warning(SAXParseException e) throws SAXException {
  16. System.out.println("WARNING : " + e.getMessage()); // do nothing
  17. }
  18.  
  19. @Override
  20. public void error(SAXParseException e) throws SAXException {
  21. System.out.println("ERROR : " + e.getMessage());
  22. throw e;
  23. }
  24.  
  25. @Override
  26. public void fatalError(SAXParseException e) throws SAXException {
  27. System.out.println("FATAL : " + e.getMessage());
  28. throw e;
  29. }
  30. }
  31. );
  32.  
  33. reader.parse(new InputSource( xml ));
  34.  
  35. return true;
  36.  
  37. }
  38. catch (ParserConfigurationException | IOException pce) {
  39.  
  40. throw pce;
  41.  
  42. }
  43. catch (SAXException se){
  44.  
  45. return false;
  46.  
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement