import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import java.io.IOException; import javax.swing.JTree; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SAXCompact { private static JTree jtree = new JTree(); public static void main(String argv[]) { if (argv.length != 1) { System.out.println("Usage: java SAXCompact file"); return; } //XMLParser creation SAXParserFactory factory; SAXParser saxParser; try { factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); saxParser = factory.newSAXParser(); saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); // parse file and print compact form saxParser.parse(argv[0], new CompactHandler()); System.out.println(); // parse file and build a tree form saxParser.parse(argv[0],new JTreeHandler(jtree)); // display the built tree new TreeViewer(jtree).show(); } catch (ParserConfigurationException e) { System.out.println("Bad parser configuration"); } catch (SAXParseException e) { System.out.println(argv[0]+" is not well-formed"); System.out.println(e.getMessage()+ " at line "+e.getLineNumber()+ ", column "+e.getColumnNumber()); } catch (SAXException e){ System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("IO Error on "+argv[0]); } } // main(String[]) } // class SAXCompact