Sax只能读出xml文件中的内容Sax是目前解析xml速度最快的Sax是基于事件的(observer):DOM先读取在解析,(我们将通过工厂实例创建DOM解析器实例)DOM解析工厂(DocumentBuilderFactory)(DocumentBuilder),调用DOM解析器的parse方法,将XML转换成为一个Document对象(Document),;;;.;.;.;.;lassDomParse{//DOM解析方法publicvoidparse(InputStreamin)throwsException{//创建一个文档解析工厂实例DocumentBuilderFactorydbf=();//根据解析工厂创建一个解析器DocumentBuilderdb=();//将解析器将文件流转换为文档对象Documentdoc=(in);//根据文档对象查找到XML的根节点Elementroot=();//用循环将所的节点打印出来for(;.;.;.;lassEventParseextendsDefaultHandler{publicEventParse(){super();}//载入文档时回调些函数publicvoidstartDocument()throwsSAXException{}//当标签中有内容时,回调函数publicvoidcharacters(char[]ch,intstart,intlength)throwsSAXException{}//当文档解析结束时,回调此函数publicvoidendDocument()throwsSAXException{}//开始解析标记元素时,回调些函数publicvoidstartElement(Stringuri,StringlocalName,StringqName,Attributesattributes)throwsSAXException{}};;;lassTestEventParse{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubtry{//创建SAX解析工厂实例SAXParserFactoryspf=();//得到SAX解析器SAXParsersp=();//创建事件实例EventParseep=newEventParse();//((""),ep);}catch(Exceptionex){("解析出错:"+());}}}DOM4j解析dom4j是一个Java的XMLAPI,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的JavaXMLAPI,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,e上找到它。在IBMdeveloperWorks上面可以找到一篇文章,对主流的JavaXMLAPI进行的性能、功能和易用性的评测,dom4j无论在那个方面都是非常出色的。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一
XML解析教学 来自淘豆网m.daumloan.com转载请标明出处.