import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXParseException; public class CompactHandler extends DefaultHandler{ // production of string of spaces with a lazy StringBuffer private static StringBuffer blanks = new StringBuffer(); private String blanks(int n){ for(int i=blanks.length();i0)System.out.print('\n'+blanks(indent)); System.out.print('@'+attrs.getLocalName(i)+'[' +attrs.getValue(i)+']'); closed=true; } } public void endElement(String uri, String localName, String raw) throws SAXException { System.out.print(']'); closed = true; indent=indent-1-localName.length(); } public void characters(char[] ch, int start, int length) throws SAXException { if(closed){ System.out.print('\n'+blanks(indent)); closed = false; } String s = new String(ch,start,length).trim(); System.out.print(s); if(s.length()>0) closed=true; } // error handling... private void message(String mess,SAXParseException e) throws SAXException{ System.out.println("\n"+mess+ "\n Line:"+e.getLineNumber()+ "\n URI:" +e.getSystemId()+ "\n Message:"+e.getMessage()); } public void fatalError(SAXParseException e) throws SAXException{ message("Fatal error",e); } public void error(SAXParseException e) throws SAXException{ message("Error",e); } public void warning(SAXParseException e) throws SAXException{ message("Warning",e); } }