Android笔试总结
作者:网络转载 发布时间:[ 2013/6/4 10:00:26 ] 推荐标签:
(1)使用SAX读取XML文件;它采用的是事件驱动,并不需要解析完整个文档,速度快并且占用内存少。需要为SAX提供实现ContentHandler接口的类。
PersonDefaultHandler.java
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.sinber.domain.Person;
publicclass PersonDefaultHandler extends DefaultHandler {
private List persons;
private Person person ; //记录当前person
private String perTag; //记录前一个标签的名称
/**
* 重写父类的开始文档方法。用于初始化
*/
@Override
publicvoid startDocument() throws SAXException {
persons =new ArrayList();
}
@Override
publicvoid startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if("person".equals(localName)){
Integer id =new Integer(attributes.getValue(0)); //取id
person =new Person();
person.setId(id);
}
perTag = localName;
}
/**参数:
* ch 整个XML字符串
* start 节点值在整个XML字符串中的索引位置
* length 节点值的长度
*/
@Override
publicvoid characters(char[] ch, int start, int length)
throws SAXException {
if(perTag!=null){
String data =new String(ch,start,length);
if("name".equals(perTag)){
person.setName(data);
}elseif("age".equals(perTag)){
person.setAge(new Short(data));
}
}
}
@Override
publicvoid endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
person =null;
}
perTag =null;
}
public List getPersons() {
return persons;
}
}
SAXPerson.java
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.sinber.domain.Person;
publicclass SAXPerson{
publicstatic List getPerson() throws Exception{
//通过类装载器获取文件
InputStream inStream = SAXPersonService.class.getClassLoader().getResourceAsStream("person.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
PersonDefaultHandler handler =new PersonDefaultHandler();
saxParser.parse(inStream, handler);
inStream.close();
return handler.getPersons();
}
}
(2)DOM解析XML文件时,会将XML文件的所有内容读取到内存中,然后允许您使用DOM API遍历XML树、检索所需的数据。
DOMPerson.java
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sinber.domain.Person;
publicclass DOMPerson {
publicstatic List getPerson() throws Exception{
List pers =new ArrayList();
InputStream inStream = SAXPersonService.class.getClassLoader().getResourceAsStream("person.xml");
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inStream);
Element root = dom.getDocumentElement();
NodeList persons = root.getElementsByTagName("person");
for(int i=0;i
Element personNode =(Element)persons.item(i);
Person person =new Person();
person.setId(new Integer(personNode.getAttribute("id")));
NodeList childNodes = personNode.getChildNodes();
for(int j=0;j
Node childNode = childNodes.item(j);
if(childNode.getNodeType()==Node.ELEMENT_NODE){
Element element = (Element)childNode;
if("name".equals(childNode.getNodeName())){
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11