qq_最后的轻语_0
2016-06-26 16:55
package XMLSAX2; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXParserHander extends DefaultHandler { int studentindex=0; String value=null; Person person=null; private ArrayList<Person> personList=new ArrayList<Person>(); public ArrayList<Person> getPersonList() { return personList; } @Override public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { // TODO Auto-generated method stub super.startElement(arg0, arg1, arg2, arg3); if(arg2.equals("student")){ person=new Person(); studentindex++; System.out.println("=====开始遍历第"+studentindex+"学生====="); } if(arg2.equals("name")){ System.out.print("属性名为:"+arg2); } if(arg2.equals("age")){ System.out.print("属性名为:"+arg2); } if(arg2.equals("number")){ System.out.print("属性名为:"+arg2); } } @Override public void endElement(String arg0, String arg1, String arg2) throws SAXException { // TODO Auto-generated method stub super.endElement(arg0, arg1, arg2); if(arg2.equals("student")){ personList.add(person); System.out.println(person.getAge()+" "+person.getName()+" "+person.getNumber()); System.out.println("=====结束遍历第"+studentindex+"学生====="); person=null; } if(arg2.equals("name")){ person.setName(value); } if(arg2.equals("age")){ person.setAge(value); } if(arg2.equals("number")){ person.setNumber(value); } } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); System.out.println("开始遍历xml文件"); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); System.out.println("结束遍历xml文件"); } @Override public void characters(char[] arg0, int arg1, int arg2) throws SAXException { // TODO Auto-generated method stub super.characters(arg0, arg1, arg2); value=new String(arg0,arg1,arg2); if(!value.trim().equals("")){ System.out.println("-----属性值为:"+value); } } } 测试的时候 xml内容可以存入对象当中,可是new SAXParserHander().getArrayList().size() 输出为0?
SAXParserHander类里头getArrayList()的方法在哪?
Java眼中的XML---文件读取
83202 学习 · 431 问题
相似问题