• 使用SAX解析XML文件


    关于使用SAX解析XML文件也没什么要说明的,直接上代码吧。

    关键如下:

    public class PersonHandler extends DefaultHandler {
    	private String preTAG;
    	private List<PersonInfo> personList;
    	private PersonInfo per;
    
    	public PersonHandler() {
    		super();
    	}
    
    	public PersonHandler(List<PersonInfo> personList) {
    		super();
    		this.personList = personList;
    	}
    
    	public void startElement(String uri, String localName, String qName,
    			Attributes attributes) throws SAXException {
    		preTAG = localName;
    		if ("person".equals(localName)) {
    			per = new PersonInfo();
    			per.setId(new Integer(attributes.getValue(0)));
    		}
    		super.startElement(uri, localName, qName, attributes);
    	}
    
    	public void endElement(String uri, String localName, String qName)
    			throws SAXException {
    		preTAG = "";
    		if ("person".equals(localName)) {
    			personList.add(per);
    		}
    		super.endElement(uri, localName, qName);
    	}
    
    	public void characters(char[] ch, int start, int length)
    			throws SAXException {
    		if ("name".equals(preTAG)) {
    			per.setName(new String(ch, start, length));
    		} else if ("age".equals(preTAG)) {
    			per.setAge("" + Integer.parseInt(new String(ch, start, length)));
    		}
    		super.characters(ch, start, length);
    	}
    
    	public List<PersonInfo> getPersonList() {
    		return personList;
    	}
    
    	public void setPersonList(List<PersonInfo> personList) {
    		this.personList = personList;
    	}
    }
    这里需要一个辅助类:

    public class PersonInfo {
    
    	private Integer id;
    	private String name;
    	private String age;
    
    	public PersonInfo() {
    
    	}
    
    	public PersonInfo(Integer id, String name, String age) {
    		this.id = id;
    		this.name = name;
    		this.age = age;
    	}
    
    	public Integer getId() {
    		return id;
    	}
    
    	public void setId(Integer id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getAge() {
    		return age;
    	}
    
    	public void setAge(String age) {
    		this.age = age;
    	}
    
    }
    
    主界面Activity入口如下:

    public class SAXActivity extends Activity {
    	Button btn;
    	ListView listView;
    	List<String> list = new ArrayList<String>();
    
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		btn = (Button) findViewById(R.id.sax_button);
    		listView = (ListView) findViewById(R.id.lv);
    		btn.setOnClickListener(new OnClickListener() {
    			public void onClick(View arg0) {
    				List<PersonInfo> persons = parserXMl();
    				for (Iterator iterator = persons.iterator(); iterator.hasNext();) {
    					PersonInfo person = (PersonInfo) iterator.next();
    					list.add(String.valueOf("ID:" + person.getId()) + " 姓名: "
    							+ person.getName() + " 年龄: " + person.getAge());
    				}
    				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    						getApplicationContext(),
    						android.R.layout.simple_list_item_1, list);
    				listView.setAdapter(adapter);
    			}
    		});
    	}
    
    	private List<PersonInfo> parserXMl() {
    		List<PersonInfo> persons = new ArrayList<PersonInfo>();
    		PersonInfo person = null;
    		try {
    			// 创建解析器
    			SAXParserFactory sax = SAXParserFactory.newInstance();
    			XMLReader reader = sax.newSAXParser().getXMLReader();
    			// 为reader设置内容处理器
    			reader.setContentHandler(new PersonHandler(persons));
    			// 开始解析文件
    			reader.parse(new InputSource(getResources().openRawResource(R.raw.person)));
    			// 循环保存解析内容到persons
    			for (Iterator iterator = persons.iterator(); iterator.hasNext();) {
    				person = (PersonInfo) persons.iterator();
    			}
    			persons.add(person);
    		} catch (Exception e) {
    		}
    		return persons;
    	}
    }
    解析结果如下:





  • 相关阅读:
    SCRAM
    package-info.java https://www.intertech.com/Blog/whats-package-info-java-for/
    mybatis-3 cache 源码赏析
    Cache replacement policies 缓存实现算法
    MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参
    Taking a peek inside with the Actuator
    表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
    Why is long2ip conversion important?
    遇到的1/3,在十进位制中是一个无限小数,但在这种进位制中就是一个有限小数。
    并发编程:Actors 模型和 CSP 模型
  • 原文地址:https://www.cnblogs.com/fengju/p/6336139.html
Copyright © 2020-2023  润新知