(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())){