student.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8" ?> <students> <student id="800"> <name>Sherlock Holmes</name> <address>221B Baker Street</address> </student> <student id="801"> <name>John Watson</name> <address>221B Baker Street</address> </student> </students> |
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Student { private int id; private String name; private String address; public Student(int id, String name, String address){ this.id = id; this.name = name; this.address = address; } public String toString(){ return name + " (" + id + ") lives at " + address; } } |
StudentXMLParser.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; import java.util.ArrayList; public class StudentXMLParser extends DefaultHandler { ArrayList<Student> students; String stringBuffer; // Temporary global state int id; String name; String address; public void startDocument() throws SAXException { System.out.println("Start Document"); students = new ArrayList<>(); } public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { System.out.println("Start Element " + qName); if (qName.equals("student")){ String idAttr = atts.getValue("id"); System.out.println("id: " + idAttr); id = Integer.parseInt(idAttr); } } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { System.out.println("End Element " + qName); if (qName.equals("name")) name = stringBuffer; else if (qName.equals("address")) address = stringBuffer; else if (qName.equals("student")){ Student s = new Student (id, name, address); students.add(s); System.out.println(s); name = address = ""; id = 0; } } public void endDocument() throws SAXException { System.out.println("End Document"); } public void characters(char ch[], int start, int length) throws SAXException { stringBuffer = new String(ch, start, length).trim(); System.out.println("Character data: " + stringBuffer); } public ArrayList<Student> getStudents(){ return students; } public static void main(String[] args){ SAXParserFactory spf = SAXParserFactory.newInstance(); try { InputStream xmlInput = new FileInputStream("student.xml"); SAXParser saxParser = spf.newSAXParser(); StudentXMLParser sxp = new StudentXMLParser(); saxParser.parse(xmlInput, sxp); for (Student s : sxp.getStudents()) System.out.println(s); } catch(SAXException|ParserConfigurationException|IOException e){ e.printStackTrace(); } } } |