12-002-3 大的XML文件解析SAX

来源:互联网 发布:知乎 唯一视觉怎么样 编辑:程序博客网 时间:2024/05/01 12:35

图文版:http://note.youdao.com/yws/public/redirect/share?id=0a3cb7361fd8684a62ecb7226c4096ab&type=false

资源文件下载:https://yunpan.cn/OcRU7u3ftuURJy  访问密码 1b25


3.1 上面的XML文件以dom形式操作时,xml文件是要全部一次性读取到内存中的,所以文件过大不适合使用DOM解析,如此就需要用到W3C之外的SAX解析,并且现在WEB的各种框架对于XML文档的解析也都是使用SAX解析来实现的,在SAX中有一个很重要的类以及它所包含的方法
org.xml.sax.helpers.DefaultHandler
   public void startDocument()  throws SAXException
   public void endDocument()  throws SAXException
   public void startElement(String uri, String localName,  String qName, Attributes attributes) 
                                          throws SAXException
   public void endElement(String uri, String localName,  String qName) 
                                          throws SAXException
   public void characters(char[] ch,  int start,  int length) 
                                           throws SAXException

3.2 SAX对xml文档将会轮流顺序的调用startElement()、characters()、endElement()三个方法

    下面来具体实现SAX解析,首先需要定义一个SAX解析器,此解析器需要继承DefaultHandler类,然后复写方法

public class SAXParse extends DefaultHandler {
 public void startDocument() throws SAXException {
  System.out.println("start document");
 }
 public void endDocument() throws SAXException {
  System.out.println("end document");
 }
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  System.out.println("start element "+qName);
 }
 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  System.out.println("end element "+qName);
 }
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  System.out.println("the current text "+new String(ch,start,length));
 }
}

    SAX解析工厂类可以用来获取指定的XML文件

javax.xml.parsers.SAXParserFactory
  public static SAXParserFactory newInstance() ;
  public abstract SAXParser newSAXParser()  throws ParserConfigurationException,  SAXException
javax.xml.parsers.SAXParser 
  public void parse(File f,  DefaultHandler dh) throws SAXException,  IOException
  public void parse(InputStream is, DefaultHandler dh) throws SAXException,  IOException

3.3 现在开始进行XML文件的操作

public class MySAX {
 public static void main(String[] args) throws Exception{
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = factory.newSAXParser() ;
  File file = new File("F:"+File.separator+"project"+File.separator+"javaEE"+File.separator+"xml"+File.separator+"comm.xml") ;
  parser.parse(file, new SAXParse());
 }
}

 3.4 利用SAX输出一个xml文件

public class SAXParse extends DefaultHandler {
 public void startDocument() throws SAXException {
  System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>");
 }
 public void endDocument() throws SAXException {
 }
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  System.out.print("<"+qName+
    (attributes.getValue("id")==null?"":" id=\""+attributes.getValue("id")+"\"")
    +">");
 }
 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  System.out.print("</"+qName+">");
 }
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  System.out.print(new String(ch,start,length));
 }
}

  以下是输出的结果

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<contact>
 <member id="001">
  <name>小张</name>
  <age>26</age>
 </member>
 <member id="002">
  <name>小明</name>
  <age>28</age>
 </member>
</contact>
3.5 但是在实际开发中很少用到上面的方式,如果真要使用也是通过集合的形式操作,其中集合的类就是一个简单java类
,现在定义一个Member的简单java类
public class Member implements Serializable {
private String name ;
 private Integer age ;
 private String id ;
//省略getter、setter方法
}

3.6 下面编写SAX解析,所有的对象内容都以Member来保存

public class SAXParse extends DefaultHandler {
 private String currentNodeName = null ;
 private List<Member> memberList = null ;
 private Member member = null ;
 public void startDocument() throws SAXException {
  memberList = new ArrayList<Member>() ;
 }
 public void endDocument() throws SAXException {
 }
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  if("member".equals(qName)){
   member.setId(attributes.getValue("id"));
  }
  this.currentNodeName = qName ;
 }
 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  if("member".equals(qName)){
   memberList.add(member) ;
  }
  }
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  if("name".equals(this.currentNodeName)){
   this.member.setName(new String(ch,start,length));
  }else if("age".equals(this.currentNodeName)){
   this.member.setAge(Integer.parseInt(new String(ch,start,length)));
  }
 }
 public List<Member> getMemberList(){
  return this.memberList ;
 }
}


public class MySAX {
 public static void main(String[] args) throws Exception{
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = factory.newSAXParser() ;
  File file = new File("F:"+File.separator+"project"+File.separator+"javaEE"+File.separator+"xml"+File.separator+"comm.xml") ;
  SAXParse sax = new SAXParse() ;
  parser.parse(file, sax);
  List<Member> list = sax.getMemberList() ;
  Iterator<Member> iter = list.iterator() ;
  while(iter.hasNext()){
   Member vo = iter.next() ;
   System.out.println(vo.getId()+","+vo.getName()+","+vo.getAge()) ;
  }
 }
}

3.7 SAX操作只能进行XML的解析(读取)而不能进行输出(本地文件的保存)


0 0
原创粉丝点击