Most visited

Recently visited

Added in API level 1

SAXParserFactory

public abstract class SAXParserFactory
extends Object

java.lang.Object
   ↳ javax.xml.parsers.SAXParserFactory


定义一个工厂API,使应用程序能够配置和获取基于SAX的解析器来解析XML文档。

Summary

Protected constructors

SAXParserFactory()

受保护的构造函数强制使用 newInstance()

Public methods

abstract boolean getFeature(String name)

返回org.xml.sax.XMLReader的基础实现中请求的特定属性。

Schema getSchema()

获取通过 setSchema(Schema)方法指定的 Schema对象。

boolean isNamespaceAware()

指示工厂是否配置为生成可识别名称空间的解析器。

boolean isValidating()

指示工厂是否配置为生成在解析期间验证XML内容的解析器。

boolean isXIncludeAware()

获取XInclude处理的状态。

static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader)

返回 SAXParserFactory的命名实现的 SAXParserFactory

static SAXParserFactory newInstance()

返回Android的实现 SAXParserFactory

abstract SAXParser newSAXParser()

使用当前配置的工厂参数创建SAXParser的新实例。

abstract void setFeature(String name, boolean value)

设置org.xml.sax.XMLReader底层实现中的特定功能。

void setNamespaceAware(boolean awareness)

指定由此代码生成的解析器将提供对XML名称空间的支持。

void setSchema(Schema schema)

Schema设置为由此工厂创建的解析器使用。

void setValidating(boolean validating)

指定由此代码生成的解析器将在分析文档时验证文档。

void setXIncludeAware(boolean state)

设置XInclude处理的状态。

Inherited methods

From class java.lang.Object

Protected constructors

SAXParserFactory

Added in API level 1
SAXParserFactory ()

Protected constructor to force use of newInstance().

Public methods

getFeature

Added in API level 1
boolean getFeature (String name)

返回org.xml.sax.XMLReader的基础实现中请求的特定属性。

Parameters
name String: The name of the property to be retrieved.
Returns
boolean Value of the requested property.
Throws
ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.

也可以看看:

getSchema

Added in API level 8
Schema getSchema ()

获取通过 setSchema(Schema)方法指定的 Schema对象。

Returns
Schema the Schema object that was last set through the setSchema(Schema) method, or null if the method was not invoked since a SAXParserFactory is created.
Throws
UnsupportedOperationException For backward compatibility, when implementations for earlier versions of JAXP is used, this exception will be thrown.

isNamespaceAware

Added in API level 1
boolean isNamespaceAware ()

指示工厂是否配置为生成可识别名称空间的解析器。

Returns
boolean true if the factory is configured to produce parsers which are namespace aware; false otherwise.

isValidating

Added in API level 1
boolean isValidating ()

指示工厂是否配置为生成在解析期间验证XML内容的解析器。

Returns
boolean true if the factory is configured to produce parsers which validate the XML content during parse; false otherwise.

isXIncludeAware

Added in API level 1
boolean isXIncludeAware ()

Get state of XInclude processing.

Returns
boolean current state of XInclude processing
Throws
UnsupportedOperationException For backward compatibility, when implementations for earlier versions of JAXP is used, this exception will be thrown.

newInstance

Added in API level 9
SAXParserFactory newInstance (String factoryClassName, 
                ClassLoader classLoader)

返回 SAXParserFactory的命名实现的 SAXParserFactory

Parameters
factoryClassName String
classLoader ClassLoader
Returns
SAXParserFactory
Throws
FactoryConfigurationError if factoryClassName is not available or cannot be instantiated.

newInstance

Added in API level 1
SAXParserFactory newInstance ()

返回Android的实现SAXParserFactory 与其他Java实现不同,此方法不涉及系统属性,属性文件或服务API。

Returns
SAXParserFactory a new SAXParserFactory.
Throws
FactoryConfigurationError never. Included for API compatibility with other Java implementations.

newSAXParser

Added in API level 1
SAXParser newSAXParser ()

使用当前配置的工厂参数创建SAXParser的新实例。

Returns
SAXParser A new instance of a SAXParser.
Throws
ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
SAXException for SAX errors.

setFeature

Added in API level 1
void setFeature (String name, 
                boolean value)

设置org.xml.sax.XMLReader底层实现中的特定功能。 核心功能和属性列表可在http://www.saxproject.org/找到

所有实现都需要支持FEATURE_SECURE_PROCESSING功能。 当功能是

  • true: the implementation will limit XML processing to conform to implementation limits. Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources. If XML processing is limited for security reasons, it will be reported via a call to the registered fatalError(SAXParseException). See SAXParser parse methods for handler specification.
  • When the feature is false, the implementation will processing XML according to the XML specifications without regard to possible implementation limits.

Parameters
name String: The name of the feature to be set.
value boolean: The value of the feature to be set.
Throws
ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.
NullPointerException If the name parameter is null.

也可以看看:

setNamespaceAware

Added in API level 1
void setNamespaceAware (boolean awareness)

指定由此代码生成的解析器将提供对XML名称空间的支持。 默认情况下,这个值设置为false

Parameters
awareness boolean: true if the parser produced by this code will provide support for XML namespaces; false otherwise.

setSchema

Added in API level 8
void setSchema (Schema schema)

Schema设置为由此工厂创建的解析器使用。

Schema非空时,解析器将使用从其创建的验证程序验证文档,然后将信息传递给应用程序。

当验证程序发现警告/错误/致命错误时,解析器必须处理它们,就好像这些错误是由解析器本身发现的一样。 换句话说,如果设置了用户指定的ErrorHandler ,则必须接收这些错误,如果不是,则必须根据实现特定的默认错误处理规则对待它们。

验证器可以修改SAX事件流(例如,通过添加文档中缺失的默认值),并且解析器负责确保应用程序将接收这些修改的事件流。

最初, null被设置为 Schema

即使 isValidating()方法返回 false该处理也会生效。

http://java.sun.com/xml/jaxp/properties/schemaSource属性和/或http://java.sun.com/xml/jaxp/properties/schemaLanguage属性与非空Schema对象一起使用是Schema 这样的配置将导致SAXException时这些属性上设置例外SAXParser

Note for implementors

解析器必须能够使用任何Schema实现。 但是,只要分析器和模式产生规范中描述的结果,就可以使用特定于实现的自定义机制。

Parameters
schema Schema: Schema to use, null to remove a schema.
Throws
UnsupportedOperationException For backward compatibility, when implementations for earlier versions of JAXP is used, this exception will be thrown.

setValidating

Added in API level 1
void setValidating (boolean validating)

指定由此代码生成的解析器将在分析文档时验证文档。 默认情况下,其值设置为false

请注意,这里的“验证”是指XML建议中定义的a validating parser 换句话说,它本质上只是控制DTD验证。 (除了在JAXP 1.2中定义的传统两个属性,请参阅here了解更多详细信息。)

要使用现代模式语言(如W3C XML Schema或RELAX NG代替DTD),可以通过保留 setValidating(boolean)方法 false将分析器配置为非验证分析器,然后使用 setSchema(Schema)方法将模式关联到分析器。

Parameters
validating boolean: true if the parser produced by this code will validate documents as they are parsed; false otherwise.

setXIncludeAware

Added in API level 1
void setXIncludeAware (boolean state)

设置XInclude处理的状态。

如果在文档实例中找到XInclude标记,是否按照 XML Inclusions (XInclude) Version 1.0中的规定处理。

XInclude处理默认为 false

Parameters
state boolean: Set XInclude processing to true or false
Throws
UnsupportedOperationException For backward compatibility, when implementations for earlier versions of JAXP is used, this exception will be thrown.

Hooray!