xml sax解析
- - 移动开发 - ITeye博客最近一直在做接口,主要用对xml的解析用的是sax,下面我对sax的几种写法做了一个测试:. System.out.println("耗时:"+(end-start));. System.out.println("当前 Java 虚拟机中的使用内存量:" + (freeMemory01-freeMemory02) + " 字节");.
public class TestSax { MyHandler handler; public List<Student> inFile01(String str){ SAXParserFactory spf=SAXParserFactory.newInstance(); SAXParser sap; try { sap = spf.newSAXParser(); handler=new MyHandler(); sap.parse(str, handler); } catch (Exception e){ } return handler.getList(); } public List<Student> inFile02(File file){ InputStream is; SAXParserFactory spf=SAXParserFactory.newInstance(); SAXParser sap; try { is = new FileInputStream(file); sap = spf.newSAXParser(); handler=new MyHandler(); sap.parse(is, handler); } catch(Exception e){ } return handler.getList(); } public List<Student> inFile03(File file){ InputStream is; SAXParserFactory spf=SAXParserFactory.newInstance(); SAXParser sap; try { is = new FileInputStream(file); sap = spf.newSAXParser(); XMLReader xr=sap.getXMLReader(); handler=new MyHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(is)); } catch(Exception e){ } return handler.getList(); } public static void main(String args[]) throws FileNotFoundException{ TestSax ts=new TestSax(); long start=System.currentTimeMillis(); long freeMemory01 =Runtime.getRuntime().freeMemory(); //List<Student> list=ts.inFile02(new File("F:/student.xml")); List<Student> list=ts.inFile01("F:/student.xml"); long end=System.currentTimeMillis(); long freeMemory02 =Runtime.getRuntime().freeMemory(); System.out.println("耗时:"+(end-start)); System.out.println("当前 Java 虚拟机中的使用内存量:" + (freeMemory01-freeMemory02) + " 字节"); for(Student stu:list){ System.out.println(stu.getName()); System.out.println(stu.getAge()); System.out.println(stu.getEnglish()); } } }
public class MyHandler extends DefaultHandler { Student st=null; String preTag = null; String currentTag=null; List<Student> list; @Override public void startDocument() throws SAXException { super.startDocument(); list=new ArrayList<Student>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); currentTag=qName; if(currentTag.equals("info")) st=new Student(); preTag=qName; //将正在解析的节点名称赋给preTag } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); String text = new String(ch, start, length); if(preTag==null) return; if(currentTag.equals("name")) { st.setName(text); }else if(currentTag.equals("age")){ st.setAge(Integer.parseInt(text)); }else if(currentTag.endsWith("english")){ st.setEnglish(Integer.parseInt(text)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if("info".equals(qName)){ list.add(st); st=null; } preTag=null; } public List<Student> getList() { return list; } }
public class Student { public Student(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getEnglish() { return English; } public void setEnglish(int english) { English = english; } private String name; private int age; private int English; }
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <students> <info> <name>Lily</name> <age>18</age> <english>60</english> </info> <info> <name>Bobi</name> <age>19</age> <english>70</english> </info> <info> <name>Lucky</name> <age>20</age> <english>80</english> </info> <info> <name>Lucy</name> <age>21</age> <english>80</english> </info> <info> <name>Amy</name> <age>23</age> <english>90</english> </info> <info> <name>Jeff</name> <age>20</age> <english>56</english> </info> <info> <name>Michel</name> <age>25</age> <english>56</english> </info> <info> <name>Jay</name> <age>30</age> <english>56</english> </info> </students>