xml解析(dom和sax的区别)

来源:互联网 发布:windows录屏软件 编辑:程序博客网 时间:2024/04/29 07:54

dom(document object model):w3c标准的解析方法,解析时,首先生成dom树。接口由多个厂商实现,比如apache,ibm,sun也有自己的dom解析器。
代码规范:
 DocumentBuilderFactory  dbf = new DocumentBuilderFactory.newInstance();
 DocumentBuilder  db = dbf.newDocumentBuilder();
 Document  doc = db.parse(File f);

dom示意图:

           node
     /|/
 attr character document documenttype element notaion processing
  data            instruction
                 |
                 //
             text comment  documentfragment  entity  entityrefernce
               |
     cdata
    section
sax(simple api for xml):在读入文档是生成相应的事件......

代码:

 
import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserTest {
 public static void main(String[] args) throws Exception{
  SAXParserFactory factory=SAXParserFactory.newInstance();
  SAXParser parser=factory.newSAXParser();
  parser.parse(new File("test01.xml"),new DefaultHandler(){

   public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
    System.out.println(new String(arg0,arg1,arg2));
   }

   public void endElement(String arg0, String arg1, String arg2) throws SAXException {
    System.out.println("</"+arg2+">");
   }

   public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
    System.out.print("<"+arg2);
    for(int i=0;i<arg3.getLength();i++){
     System.out.print(" "+arg3.getQName(i)+"=/""+arg3.getValue(i)+"/"");
    }
    System.out.println(">");
   }
   
  });
 }

}


dom解析可以满足我们的大多数目的,如果你要处理很长的文档,或者只是对部分元素感兴趣,而不关系上下文,建议使用sax。