import java.io.File; import javax.swing.JFileChooser; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XMLReader extends DefaultHandler{ private SAXParser parser; public XMLReader(File file) throws Exception{ //create a parser SAXParserFactory factory = SAXParserFactory.newInstance(); parser = factory.newSAXParser(); parser.parse(file, this); } @Override public void startDocument() throws SAXException { super.startDocument(); System.out.println("Start the document."); } @Override public void endDocument() throws SAXException { super.endDocument(); System.out.println("End the document."); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName + " start"); super.startElement(uri, localName, qName, attributes); } public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(qName + " end"); super.endElement(uri, localName, qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // contents between tags String line = new String(ch); System.out.println(line); super.characters(ch, start, length); } /** * @param args */ public static void main(String[] args) { JFileChooser chooser = new JFileChooser("."); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION){ //the user hit "ok" //get the selected file File file = chooser.getSelectedFile(); try { XMLReader reader = new XMLReader(file); } catch(Exception e){ e.printStackTrace(); } } } }