




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1,第5章SAX,TheSimpleAPIforXML,2,在应用程序中使用XML文档,3,SAX的特点,通过触发事件,顺序访问元素已经分析过的数据,不能再返回处理内存占用量较少,基本固定事实标准,而非W3C标准最初版本SAX1.0当前版本SAX2.0,4,SAX语法分析体系结构,SAXParserFactory,SAXParser,XML,SAXReader,ContentHandler,ErrorHandler,DTDHandler,EntityHandler,5,获取SAX解析器,javax.xml.parsers.SAXParserFactory;javax.xml.parsers.SAXParser;javax.xml.parsers.ParserConfigurationException;/gettheparserSAXParserFactoryfactory=SAXParserFactory.newInstance();SAXParsersaxParser=factory.newSAXParser();/parsethedocumentsaxParser.parse(newFile(argv0),handler);,6,将某个handler传送给解析器,NotethataneventhandlerhastobepassedtotheSAXparser.Thismustimplementtheinterfaceorg.xml.sax.ContentHandler;Easiertoextendtheadapterorg.xml.sax.helpers.DefaultHandler,7,OverridingHandlermethods,MostimportantmethodstooverridevoidstartDocument()CalledoncewhendocumentparsingbeginsvoidendDocument()CalledoncewhenparsingendsvoidstartElement(.)CalledeachtimeanelementbegintagisencounteredvoidendElement(.)Calledeachtimeanelementendtagisencounteredvoidcharacters(.)CalledrandomlybetweenstartElementandendElementcallstoaccumulatedcharacterdata,8,startElement,publicvoidstartElement(StringnamespaceURI,/ifnamespaceassocStringsName,/nonqualifiednameStringqName,/qualifiednameAttributesattrs)/listofattributesAttributeinfoisobtainedbyqueryingAttributesobjects.,9,Characters,publicvoidcharacters(charbuf,/bufferofcharsaccumulatedintoffset,/beginelementofcharsintlen)/numberofcharsNote,charactersmaybecalledmorethanoncebetweenbegintag/endtagAlso,mixed-contentelementsrequirecarefulhandling,10,SAX应用示例(1/9),作为输入的XML文档KilpelinenPekkaMttnenMattiMttnenMaijaRmppnenMaija,11,SAX应用示例1(2.1/9),要求的输出如下:PekkaKilpelinen(1234)MattiMttnen(5678)MaijaMttnen(9012)MaijaRmppnen(3456),12,SAX应用示例1(2.2/9),事件驱动的处理策略从元素person开始,记录idnum(e.g.,1234)记录各个last与first元素的开始与结束,以便存储其元素内容(e.g.,“Kilpelinen”和Pekka)每一个person元素结束时,输出与该元素有关的数据,13,SAX应用示例1(3/9),Application:Firstimportrelevantinterfaces,14,SAX应用示例1(4/9),Implementrelevantcall-backmethods:publicclassSAXDBAppextendsDefaultHandler/Flagstorememberelementcontext:privatebooleanInFirst=false,InLast=false;/Storageforelementcontentsand/attributevalues:privateStringFirstName,LastName,IdNum;,15,SAX应用示例1(5/9),Call-backmethods:recordthestartoffirstandlastelements,andtheidnumattributeofaperson:publicvoidstartElement(StringnsURI,StringlocalName,StringrawName,Attributesatts)if(rawName.equals(person)IdNum=atts.getValue(idnum);if(rawName.equals(first)InFirst=true;if(rawName.equals(last)InLast=true;/startElement,16,SAX应用示例1(6/9),Call-backmethodscontinue:Recordthetextcontentofelementsfirstandlast:publicvoidcharacters(charbuf,intstart,intlength)if(InFirst)FirstName=newString(buf,start,length);if(InLast)LastName=newString(buf,start,length);/characters,17,SAX应用示例1(7/9),Attheendofperson,outputthecollecteddata:publicvoidendElement(StringnsURI,StringlocalName,StringqName)if(qName.equals(person)System.out.println(FirstName+LastName+(+IdNum+);/Updatethecontextflags:if(qName.equals(first)InFirst=false;/(andthesameforlastandInLast),18,SAX应用示例1(8/9),Applicationmainmethod:publicstaticvoidmain(Stringargs)throwsException/InstantiateanXMLReader(fromJAXP/SAXParserFactory):SAXParserFactoryspf=SAXParserFactory.newInstance();trySAXParsersaxParser=spf.newSAXParser();XMLReaderxmlReader=saxParser.getXMLReader();,19,SAX应用示例1(9/9),Mainmethodcontinues:/Instantiateandpassanew/ContentHandlertoxmlReader:ContentHandlerhandler=newSAXDBApp();xmlReader.setContentHandler(handler);for(inti=0;iargs.length;i+)xmlReader.parse(argsi);catch(Exceptione)System.err.println(e.getMessage();System.exit(1);/main,20,SAX应用示例2,21,SampleXMLDocument,taatttctcccattttgtaggttatcacttcactctgttgactttcttttgtaatgcaactaaatccaggcgaagcatttcagcttaaccccgagacttttg,DocumentcontainstwosequencesofDNA.,22,StartDocumentStartElement:DASDNAStartElement:SEQUENCEStartElement:DNACharacters:taatttctcccattttgtaggttatcacttcactctgttgactttcttttgCharacters:EndElement:DNAEndElement:SEQUENCEStartElement:SEQUENCEStartElement:DNACharacters:taatgcaactaaatccaggcgaagcatttcagcttaaccccgagacttttgCharacters:EndElement:DNAEndElement:SEQUENCEEndElement:DASDNAEndDocument,SampleOutput,23,packagecom.oreilly.bioxml.sax;importorg.xml.sax.Attributes;importorg.xml.sax.ContentHandler;importorg.xml.sax.Locator;importorg.xml.sax.SAXException;importorg.xml.sax.XMLReader;importorg.xml.sax.helpers.XMLReaderFactory;importjava.io.IOException;/*BasicSAXExample.*IllustratesbasicimplementationoftheSAXContentHandler.*/publicclassSAXBasicimplementsContentHandlerpublicvoidstartDocument()throwsSAXExceptionSystem.out.println(StartDocument);,24,publicvoidcharacters(charch,intstart,intlength)throwsSAXExceptionStringstr=newString(ch,start,length);System.out.println(Characters:+str);publicvoidendDocument()throwsSAXExceptionSystem.out.println(EndDocument);publicvoidendElement(StringnamespaceURI,StringlocalName,StringqName)throwsSAXExceptionSystem.out.println(EndElement:+localName);publicvoidendPrefixMapping(Stringprefix)throwsSAXException/No-op,25,publicvoidignorableWhitespace(charch,intstart,intlength)throwsSAXException/No-oppublicvoidprocessingInstruction(java.lang.Stringtarget,java.lang.Stringdata)throwsSAXException/No-oppublicvoidsetDocumentLocator(Locatorlocator)/No-oppublicvoidskippedEntity(Stringname)throwsSAXException/No-oppublicvoidstartElement(StringnamespaceURI,StringlocalName,StringqName,Attributesatts)throwsSAXExceptionSystem.out.println(StartElement:+localName);,26,publicvoidstartPrefixMapping(Stringprefix,Stringuri)throwsSAXException/No-op/*PrintsCommandLineUsage*/privatestaticvoidprintUsage()System.out.println(usage:SAXBasicxml
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 幼儿园综合素质评价方案及实施细则
- 初中数学教学计划全集
- 2025年期货从业资格之《期货法律法规》经典例题及答案详解【典优】
- 大学物理实验教学设计参考
- 2024年施工员模考模拟试题及参考答案详解【培优B卷】
- 农民收入增加与农业生产技术研究
- 城市空气质量监测运行制度
- 度假村经理助理的职责和晋级通道
- 高中物理期末考试真题解析范例汇编
- 2 他人眼中的我教学设计-2025-2026学年小学心理健康三年级大象版
- 应急值班值守管理制度
- 外国文学史-总课件
- 《中小企业划型标准规定》补充说明
- 房屋租赁信息登记表
- 六年级上册数学课件-1.6 长方体和正方体的体积计算丨苏教版 (共15张PPT)
- 食品科学技术词汇
- 质量总监.安全生产责任制考核表
- 小学生汉字听写大赛题库
- 第一框 关爱他人
- 精品地铁车站装饰装修质量保障措施
- 渗透检测培训教材(1)
评论
0/150
提交评论