How to parse Xml file -- SAX!

Different from DOM parser, the SAX parser will parse the file from one node to another.

There are several methods are common used in SAX parser:

  startDocument()

  startElement()

  character()

  endElement()

  endDocument()

For example:

  <Books>              ------> startDocument()

    <Book>              ------> startElement()

      <name>           ------> startElement()

        General         ------> character()

      </name>            ------> endELement()

      <price>18$</price>

    </Book>               ------> endELement()

  </Books>               ------> endDocument()

How to get the SAXParser?

  //1.get the SAXPaserFactory object

  SAXParserFactory factory = SAXParserFactory.newInstance();

  //2.get the SAXParser by SAXParserFactory

  SAXParser parser = factory.newSAXParser();

  //3.use the parser to parse specific xml file

  parser.parse("xml‘s path",new DefaultHandler{

    public void startElement(String uri,String localName,String qName,Attribute attibute) throws SAXException

      

    }

    public void endElement(String uri,String localName,String qName) throws SAXException{

      

    }

    public void character(char[] chs,int start,int lenght) throws SAXException{

      

    }

  });

There is an important case: how to write the xml‘s object to JavaBean?

时间: 2024-10-10 15:17:38

How to parse Xml file -- SAX!的相关文章

How to parse Xml file -- DOM!

We can see that when we want to use the Dom4j to parse XML , we should import org.dom4j.* At the same time ,that to parse XML by DOM ,we should import org.w3c.dom.* The conclusion is that DOM and Dom4j is different and located in different jar.The DO

java.lang.IllegalArgument,Parse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml

启动Tomcat时错误:java.lang.IllegalArgumentException: Can't convert argument: nullParse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml Resolution:上述问题,是由于在Eclipse下重构JEE项目名时,Eclipse自动更新了部署文件web.xml,重新生成了xml文件的头部声明,新增加了javaee的命名空间. Xml代

XML(十)- SAX解析

XML解析之 SAX解析 SAX介绍 SAX的全称是Simple APIs for XML,也即XML简单应用程序接口. 与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式. 当使用SAX分析器对XML文档进行分析时,会触发一系列事件,并激活相应的事件处理函数,应用程序通过这些事件处理函数实现对XML文档的访问,因而SAX接口也被称作事件驱动接口. 局限性: 1. SAX分析器在对XML文档进行分析时,触发了一系列的事件,由于事件触发本身是有时序性的,因此,SAX

安卓解析XML之SAX

***MySaxHandler*** package android_xml_sax; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MySaxHand

Spring MVC 单元测试异常 Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

Sping 3.2.8.RELEASE + sping mvc + JDK 1.8运行异常. java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springfr

Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

最近在琢磨maven,自己照着网上的例子塔了一个例子,在junit测试时报错了,详细错误信息: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\workspace\MAVEN\example_1\target\classes\cn\springmvc\service\impl\UserServiceImpl.clas

[vb.net]XML File Parsing in VB.NET

Introduction Parsing XML files has always been time consuming and sometimes tricky. .NET framework provides powerful new ways of parsing XML. The various techniques know to parse xml files with .NET framework are using XmlTextReader, XmlDocument, Xml

Could not parse configuration: file:/D:/apache-tomcat-7.0.55-windows-x64/apache-tomcat-7.0.55/webapp

在做ssh项目时,启动tomcat,出现了以下的错误 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ins

递归方式 DOM 解析(parse) XML

friends.xml [html] view plain copy <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> <friends date="2012"> <!--此处为注释--> <friend id="1"> <姓名>小红</姓名&