`org.xml.sax` 是 Java 标准库中用于**简单 API for XML(SAX)** 的核心包,它提供了一组基于事件驱动的、轻量级的 XML 解析接口
org.xml.sax是 Java 标准库中用于简单 API for XML(SAX)的核心包,它提供了一组基于事件驱动的、轻量级的 XML 解析接口。SAX 是一种只读、单向、顺序流式解析方式,适用于处理大型 XML 文件(内存占用低),但不支持随机访问或修改文档结构。
该包中最关键的接口包括:
org.xml.sax.XMLReader:XML 解析器的核心接口,负责读取 XML 文档并触发事件。通常通过XMLReaderFactory.createXMLReader()或SAXParserFactory.newSAXParser().getXMLReader()获取实现(如com.sun.org.apache.xerces.internal.parsers.SAXParser)。org.xml.sax.ContentHandler:最重要的回调接口,定义了处理 XML 内容事件的方法,例如:startDocument()/endDocument()startElement(String uri, String localName, String qName, Attributes attributes)endElement(String uri, String localName, String qName)characters(char[] ch, int start, int length)
(用户需实现此接口或继承DefaultHandler)
org.xml.sax.DTDHandler:处理 DTD 相关事件(如notationDecl,unparsedEntityDecl),可选实现。org.xml.sax.EntityResolver:用于自定义外部实体解析(如重定向 DTD 或 Schema 的加载位置),常用于离线解析或安全控制(避免 XXE 攻击)。org.xml.sax.ErrorHandler:处理解析过程中的警告、错误和严重错误(warning(),error(),fatalError()),默认行为是抛出SAXException。org.xml.sax.Attributes:表示元素属性的只读集合(非接口,是接口类型,实际为AttributesImpl等实现),供startElement方法传入。
⚠️ 注意:org.xml.sax包中所有接口均为回调契约,不包含解析逻辑本身;具体解析由底层 SAX 兼容解析器(如 Xerces)实现。Java SE 自带内置 SAX 支持(无需额外依赖)。
示例简写(使用DefaultHandler):
importorg.xml.sax.*;importorg.xml.sax.helpers.DefaultHandler;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;SAXParserFactoryfactory=SAXParserFactory.newInstance();SAXParserparser=factory.newSAXParser();parser.parse(newFile("data.xml"),newDefaultHandler(){@OverridepublicvoidstartElement(Stringuri,StringlocalName,StringqName,Attributesattributes){System.out.println("Start: "+qName);}});org.xml.sax Interfaces
AttributeList This interface was deprecated in API level 9. This interface has been replaced by the SAX2 Attributes interface, which includes Namespace support.
Attributes Interface for a list of XML attributes.
ContentHandler Receive notification of the logical content of a document.
DocumentHandler This interface was deprecated in API level 9. This interface has been replaced by the SAX2 ContentHandler interface, which includes Namespace support.
DTDHandler Receive notification of basic DTD-related events.
EntityResolver Basic interface for resolving entities.
ErrorHandler Basic interface for SAX error handlers.
Locator Interface for associating a SAX event with a document location.
Parser This interface was deprecated in API level 9. This interface has been replaced by the SAX2 XMLReader interface, which includes Namespace support.
XMLFilter Interface for an XML filter.
XMLReader Interface for reading an XML document using callbacks.
Classes
HandlerBase This class was deprecated in API level 9. This class works with the deprecated DocumentHandler interface. It has been replaced by the SAX2 DefaultHandler class.
InputSource A single input source for an XML entity.
Exceptions
SAXException Encapsulate a general SAX error or warning.
SAXNotRecognizedException Exception class for an unrecognized identifier.
SAXNotSupportedException Exception class for an unsupported operation.
SAXParseException Encapsulate an XML parse error or warning.
