import java.io.IOException; import java.io.StringReader; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class StudentHandler extends DefaultHandler { public static void main(String[] args)throws Exception{ String xml = "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ " \n"+ "\n"+ "\n"+ ""; StudentHandler handler = new StudentHandler(); handler.parse(new InputSource(new StringReader(xml))); } public void parse(InputSource src) throws SAXException, IOException { XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setContentHandler(this); parser.parse(src); } @Override public void characters(char[] ch, int start, int length) throws SAXException { //if there were text as part of the elements, we would deal with it here //by adding it to a StringBuffer, but we don't have to for this task super.characters(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { //this is where we would get the info from the StringBuffer if we had to, //but all we need is attributes super.endElement(uri, localName, qName); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(qName.equals("Student")){ System.out.println(attributes.getValue("Name")); } } }