SAX - DefaultHandler

org.xml.sax.helpers.DefaultHandler 实现了 org.xml.sax.EntityResolver、org.xml.sax.DTDHandler、org.xml.sax.ContentHandler 和 org.xml.sax.ErrorHandler,并提供了默认的实现方法(不做任何操作)。

ContentHandler

setDocumentLocator

Receive an object for locating the origin of SAX document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the ContentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application‘s business rules).  The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation SAX event callbacks after startDocument returns and before endDocument is called. The application should not attempt to use it at any other time.

startDocument()

Receive notification of the beginning of a document. The SAX parser will invoke this method only once, before any other event callbacks (except for setDocumentLocator).

endDocument()

Receive notification of the end of a document.

There is an apparent contradiction between the documentation for this method and the documentation for ErrorHandler.fatalError(org.xml.sax.SAXParseException). Until this ambiguity is resolved in a future major release, clients should make no assumptions about whether endDocument() will or will not be invoked when the parser has reported a fatalError() or thrown an exception.

The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input.

startPrefixMapping

Begin the scope of a prefix-URI Namespace mapping.

The information from this event is not necessary for normal Namespace processing: the SAX XML reader will automatically replace prefixes for element and attribute names when the http://xml.org/sax/features/namespaces feature is true (the default).

There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary.

Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each other: all startPrefixMapping events will occur immediately before the corresponding startElement event, and all endPrefixMapping events will occur immediately after the corresponding endElement event, but their order is not otherwise guaranteed.

There should never be start/endPrefixMapping events for the "xml" prefix, since it is predeclared and immutable.

endPrefixMapping

End the scope of a prefix-URI mapping.

See startPrefixMapping for details. These events will always occur immediately after the corresponding endElement event, but the order of endPrefixMapping events is not otherwise guaranteed.

startElement

Receive notification of the beginning of an element.

The Parser will invoke this method at the beginning of every element in the XML document; there will be a corresponding endElement event for every startElement event (even when the element is empty). All of the element‘s content will be reported, in order, before the corresponding endElement event.

This event allows up to three name components for each element:

  1. the Namespace URI;
  2. the local name; and
  3. the qualified (prefixed) name.

Any or all of these may be provided, depending on the values of the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixesproperties:

  • the Namespace URI and local name are required when the namespaces property is true (the default), and are optional when the namespaces property is false (if one is specified, both must be);
  • the qualified name is required when the namespace-prefixes property is true, and is optional when the namespace-prefixes property is false (the default).

Note that the attribute list provided will contain only attributes with explicit values (specified or defaulted): #IMPLIED attributes will be omitted. The attribute list will contain attributes used for Namespace declarations (xmlns* attributes) only if the http://xml.org/sax/features/namespace-prefixes property is true (it is false by default, and support for a true value is optional).

Like characters(), attribute values may have characters that need more than one char value.

endElement

Receive notification of the end of an element.

The SAX parser will invoke this method at the end of every element in the XML document; there will be a corresponding startElement event for every endElement event (even when the element is empty).

For information on the names, see startElement.

characters

Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

The application must not attempt to read from the array outside of the specified range.

Individual characters may consist of more than one Java char value. There are two important cases where this happens, because characters can‘t be represented in just sixteen bits. In one case, characters are represented in a Surrogate Pair, using two special Unicode values. Such characters are in the so-called "Astral Planes", with a code point above U+FFFF. A second case involves composite characters, such as a base character combining with one or more accent characters.

Your code should not assume that algorithms using char-at-a-time idioms will be working in character units; in some cases they will split characters. This is relevant wherever XML permits arbitrary characters, such as attribute values, processing instruction data, and comments as well as in data reported from this method. It‘s also generally relevant whenever Java code manipulates internationalized text; the issue isn‘t unique to XML.

Note that some parsers will report whitespace in element content using the ignorableWhitespace method rather than this one (validating parsers must do so).

ignorableWhitespace

Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk of whitespace in element content (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

The application must not attempt to read from the array outside of the specified range.

processingInstruction

Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser must never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

Like characters(), processing instruction data may have characters that need more than one char value.

skippedEntity

Receive notification of a skipped entity. This is not called for entity references within markup constructs such as element start tags or markup declarations. (The XML recommendation requires reporting skipped external entities. SAX also reports internal entity expansion/non-expansion, except within markup constructs.)

The Parser will invoke this method each time the entity is skipped. Non-validating processors may skip entities if they have not seen the declarations (because, for example, the entity was declared in an external DTD subset). All processors may skip external entities, depending on the values of thehttp://xml.org/sax/features/external-general-entities and the http://xml.org/sax/features/external-parameter-entities properties.

ErrorHandler

warning

Receive notification of a warning.

SAX parsers will use this method to report conditions that are not errors or fatal errors as defined by the XML recommendation. The default behaviour is to take no action.

The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end.

Filters may use this method to report other, non-XML warnings as well.

error

Receive notification of a recoverable error.

This corresponds to the definition of "error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a validating parser would use this callback to report the violation of a validity constraint. The default behaviour is to take no action.

The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end. If the application cannot do so, then the parser should report a fatal error even if the XML recommendation does not require it to do so.

Filters may use this method to report other, non-XML errors as well.

fatalError

Receive notification of a non-recoverable error.

There is an apparent contradiction between the documentation for this method and the documentation for ContentHandler.endDocument(). Until this ambiguity is resolved in a future major release, clients should make no assumptions about whether endDocument() will or will not be invoked when the parser has reported a fatalError() or thrown an exception.

This corresponds to the definition of "fatal error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a parser would use this callback to report the violation of a well-formedness constraint.

The application must assume that the document is unusable after the parser has invoked this method, and should continue (if at all) only for the sake of collecting additional error messages: in fact, SAX parsers are free to stop reporting any other events once this method has been invoked.

时间: 2024-10-03 13:45:46

SAX - DefaultHandler的相关文章

Java SAX DefaultHandler

The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0. As shown briefly in the first text on SAX in this tutorial, you create a subclass of DefaultHandler and override certain inherited methods in this subclas

SAX解析XML

SAX 事件处理(事件驱动),不需要载入整个文档,流机制读取,分析XML文档 startDocument startElement character endElement endDocument 当sax扫描到文档时就调用startDocument函数,sax扫描到文档结束符时就调用endDocument函数,也就是说当sax扫描到哪个节点时就会调用相应的函数,同时还会携带节点的属性值. 当它扫描到节点内容值时就会调用character函数 Sax主要是用来对xml文件遍历,解析,不提供修改.

XML.03-DOM和SAX解析

body,td { font-family: calibri; font-size: 10pt } XML.03-DOM和SAX解析 XML的DOM解析 解析 处理 回写 XML的SAX解析 SAX和DOM方式的不同: SAX解析原理 SAX解析范例 解析xml常用的有两种方式,DMO和SAX DOM和SAX的区别: DOM: 在内存中生成树桩结构 优点是可以支持增删改查各种操作 缺点在于,如果文档过大的时候,可能会产生内存溢出的风险 SAX: 基于事件驱动,边读边解析 优点:占用内存小 缺点,

SAX解析

1DOM解析与SAX解析比较: DOM解析 SAX解析 原理: 一次性加载xml文档,不适合大容量的文件读取 原理: 加载一点,读取一点,处理一点.适合大容量文件的读取 DOM解析可以任意进行增删改成 SAX解析只能读取 DOM解析任意读取任何位置的数据,甚至往回读 SAX解析只能从上往下,按顺序读取,不能往回读 DOM解析面向对象的编程方法(Node,Element,Attribute),Java开发者编码比较简单. SAX解析基于事件的编程方法.java开发编码相对复杂. 2 SAX解析工具

解析XML文件的几种常见操作方法:DOM/SAX/DOM4j

1 <?xml version="1.0" encoding="utf-8"?> 2 <root> 3 <class name="class1"> 4 <student> 5 <name>张三</name> 6 <age>20</age> 7 <sex>男</sex> 8 </student> 9 <stude

xml多层嵌套解析方法SAX方式(包括网络请求)

启用异步类获取数据 package com.bwie.myasynctask; import java.io.IOException;import java.io.InputStream;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List; import javax.xml.parsers.ParserCon

JAVA解析XML之SAX方式

JAVA解析XML之SAX方式 SAX解析xml步骤 通过SAXParseFactory的静态newInstance()方法获取SAXParserFactory实例factory 通过SAXParserFactory实例的newSAXParser()方法返回SAXParser实例parser 创建一个类继承DefaultHandle,重写方法进行业务处理并创建这个类的实例handle 重写DefaultHandle类的方法 startElement方法用来遍历xml文件的开始标签; endEle

Android简化xml sax解析

dom解析占用内存大(我这边需要解析各种各样的kml文件,有时4-5M的kml文件使用dom解析很多手机就内存溢出了),也需要引入第三方库,所以使用相对于节省内存很多.不需引入其他库的sax解析就是很好的选择了.因为sax解析比较复杂的xml文件特别麻烦,所以整理了一个简化android sax解析的工具. 实现思路:和Android Touch事件传递机制一样,把需要子解析器解析的节点往下传递. 如果有进一步简化的方法,欢迎交流!email:[email protected]. 示例程序:ht

6. SAX解析XML

SAX解析: SAX采用事件处理的方式解析XML文件,利用 SAX 解析 XML 文档,涉及两个部分: 解析器和事件处理器 : 1. 解析器可以使用SAXP的API创建,创建出SAX解析器后, 就可以指定解析器去解析某个XML文档. 解析器采用SAX方式在解析某个XML文档时, 它只要解析到XML文档的一个组成部分,都会去调用事件处理器的一个方法, 解析器在调用事件处理器的方法时, 会把当前解析到的xml文件内容作为方法的参数传递给事件处理器. 2. 事件处理器由程序员编写,程序员通过事件处理器