public interface LSParser
org.w3c.dom.ls.LSParser |
一个对象的接口,能够从各种输入源构建或扩充DOM树。
LSParser
提供了一个用于解析XML并构建相应的DOM文档结构的API。 可以通过调用DOMImplementationLS.createLSParser()
方法获得LSParser
实例。
如[ DOM Level 3 Core ]中所述,当首次通过LSParser使文档可用时:
value
and nodeValue
attributes of an Attr
node initially return the XML 1.0 normalized value. However, if the parameters " validate-if-schema" and " datatype-normalization" are set to true
, depending on the attribute normalization used, the attribute values may differ from the ones obtained by the XML 1.0 attribute normalization. If the parameters " datatype-normalization" is set to false
, the XML 1.0 attribute normalization is guaranteed to occur, and if the attributes list does not contain namespace declarations, the attributes
attribute on Element
node represents the property [attributes] defined in [XML Information Set] . 预计异步 LSParser
对象还将实现 events::EventTarget
接口,以便可以在异步 LSParser
对象上注册事件侦听器。
由异步 LSParser
对象支持的事件是:
LSParser
finishes to load the document. See also the definition of the
LSLoadEvent
interface.
LSParser
signals progress as data is parsed. This specification does not attempt to define exactly when progress events should be dispatched. That is intentionally left as implementation-dependent. Here is one example of how an application might dispatch progress events: Once the parser starts receiving data, a progress event is dispatched to indicate that the parsing starts. From there on, a progress event is dispatched for every 4096 bytes of data that is received and processed. This is only one example, though, and implementations can choose to dispatch progress events at any time while parsing, or not dispatch them at all. See also the definition of the
LSProgressEvent
interface.
注意:本规范中定义的所有事件都使用命名空间URI "http://www.w3.org/2002/DOMLS"
。
在解析输入源时,错误通过错误处理程序( LSParser.domConfig
的“ error-handler ”参数)报告给应用程序。 本规范绝不会尝试定义解析XML或任何其他标记时可能发生的所有可能的错误,但会定义一些常见的错误情况。 本规范定义的错误和警告的类型( DOMError.type
)为:
"check-character-normalization-failure" [error]
"doctype-not-allowed" [fatal]
true
and a doctype is encountered.
"no-input-specified" [fatal]
LSInput
object.
"pi-base-uri-not-preserved" [warning]
false
and the following XML file is parsed:
<!DOCTYPE root [ <!ENTITY e SYSTEM 'subdir/myentity.ent' ]> <root> &e; </root>And
subdir/myentity.ent
contains:
<one> <two/> </one> <?pi 3.14159?> <more/>
"unbound-prefix-in-entity" [warning]
true
and an unbound namespace prefix is encountered in an entity's replacement text. Raising this warning is not enforced since some existing parsers may not recognize unbound namespace prefixes in the replacement text of entities.
"unknown-character-denormalization" [fatal]
false
and a character is encountered for which the processor cannot determine the normalization properties.
"unsupported-encoding" [fatal]
"unsupported-media-type" [fatal]
true
and an unsupported media type is encountered.
除了提出定义的错误和警告之外,实现还应该针对任何其他错误和警告情况(例如IO错误(文件未找到,权限被拒绝,...),XML格式错误等)引发实现特定的错误和警告。等等。
另请参阅 Document Object Model (DOM) Level 3 Load and Save Specification 。
Constants |
|
---|---|
short |
ACTION_APPEND_AS_CHILDREN 将解析操作的结果追加为上下文节点的子节点。 |
short |
ACTION_INSERT_AFTER 将解析操作的结果作为上下文节点的紧随其后的兄弟节点。 |
short |
ACTION_INSERT_BEFORE 将解析操作的结果作为上下文节点的前一个兄弟节点。 |
short |
ACTION_REPLACE 将上下文节点替换为解析操作的结果。 |
short |
ACTION_REPLACE_CHILDREN 将上下文节点的所有子节点替换为解析操作的结果。 |
Public methods |
|
---|---|
abstract void |
abort() 中止加载 |
abstract boolean |
getAsync()
|
abstract boolean |
getBusy()
|
abstract DOMConfiguration |
getDomConfig() 解析输入源时使用的 |
abstract LSParserFilter |
getFilter() 当提供过滤器时,实现会在构建DOM树结构时调用过滤器。 |
abstract Document |
parse(LSInput input) 解析来自 |
abstract Document |
parseURI(String uri) 从URI参考标识的位置解析XML文档[ IETF RFC 2396 ]。 |
abstract Node |
parseWithContext(LSInput input, Node contextArg, short action) 从 |
abstract void |
setFilter(LSParserFilter filter) 当提供过滤器时,实现会在构建DOM树结构时调用过滤器。 |
short ACTION_APPEND_AS_CHILDREN
将解析操作的结果追加为上下文节点的子节点。 要使此操作起作用,上下文节点必须是Element
或DocumentFragment
。
常数值:1(0x00000001)
short ACTION_INSERT_AFTER
将解析操作的结果作为上下文节点的紧随其后的兄弟节点。 要执行此操作,上下文节点的父级必须是Element
或DocumentFragment
。
常量值:4(0x00000004)
short ACTION_INSERT_BEFORE
将解析操作的结果作为上下文节点的前一个兄弟节点。 要使此操作工作,上下文节点的父级必须是Element
或DocumentFragment
。
常量值:3(0x00000003)
short ACTION_REPLACE
将上下文节点替换为解析操作的结果。 要使此操作起作用,上下文节点必须具有父级,并且父级必须是Element
或DocumentFragment
。
常量值:5(0x00000005)
short ACTION_REPLACE_CHILDREN
将上下文节点的所有子节点替换为解析操作的结果。 为了使此动作有效,上下文节点必须是Element
,一个Document
或者DocumentFragment
。
常量值:2(0x00000002)
boolean getBusy ()
true
如果 LSParser
当前正忙于加载文档,否则为 false
。
Returns | |
---|---|
boolean |
DOMConfiguration getDomConfig ()
解析输入源时使用的DOMConfiguration
对象。 这DOMConfiguration
是特定于解析操作。 从这个无参数值DOMConfiguration
对象被自动传递到DOMConfiguration
对象上的Document
时创建,或所使用的,由所述解析操作。 DOM应用程序负责DOMConfiguration
对象的任何所需参数值传递给由DOMConfiguration
对象引用的Document
对象。
除了[ DOM Level 3 Core ]中定义的DOMConfiguration接口中识别的参数之外, LSParser的DOMConfiguration
对象LSParser
添加或修改了以下参数:
"charset-overrides-xml-encoding"
true
LSInput
overrides any encoding from the protocol.
false
"disallow-doctype"
true
false
"ignore-unknown-character-denormalizations"
true
false
"infoset"
DOMConfiguration
for a description of this parameter. Unlike in [
DOM Level 3 Core] , this parameter will default to
true
for
LSParser
.
"namespaces"
true
false
"resource-resolver"
LSResourceResolver
object, or null. If the value of this parameter is not null when an external resource (such as an external XML entity or an XML schema location) is encountered, the implementation will request that the
LSResourceResolver
referenced in this parameter resolves the resource.
"supported-media-types-only"
true
false
"validate"
DOMConfiguration
for a description of this parameter. Unlike in [
DOM Level 3 Core] , the processing of the internal subset is always accomplished, even if this parameter is set to
false
.
"validate-if-schema"
DOMConfiguration
for a description of this parameter. Unlike in [
DOM Level 3 Core] , the processing of the internal subset is always accomplished, even if this parameter is set to
false
.
"well-formed"
DOMConfiguration
for a description of this parameter. Unlike in [
DOM Level 3 Core] , this parameter cannot be set to
false
.
Returns | |
---|---|
DOMConfiguration |
LSParserFilter getFilter ()
当提供过滤器时,实现会在构建DOM树结构时调用过滤器。 过滤器可以选择从正在构建的文档中移除元素,或者提前终止解析。
在应用了DOMConfiguration
参数所要求的操作之后,将调用该过滤器。 例如,如果“ validate ”设置为true
,则在调用过滤器之前完成验证。
Returns | |
---|---|
LSParserFilter |
Document parse (LSInput input)
从 LSInput
标识的资源中解析XML文档。
Parameters | |
---|---|
input |
LSInput : The LSInput from which the source of the document is to be read. |
Returns | |
---|---|
Document |
If the LSParser is a synchronous LSParser , the newly created and populated Document is returned. If the LSParser is asynchronous, null is returned since the document object may not yet be constructed when this method returns. |
Throws | |
---|---|
DOMException |
INVALID_STATE_ERR: Raised if the LSParser 's LSParser.busy attribute is true . |
LSException |
PARSE_ERR: Raised if the LSParser was unable to load the XML document. DOM applications should attach a DOMErrorHandler using the parameter " error-handler" if they wish to get details on the error. |
Document parseURI (String uri)
从URI参考标识的位置解析XML文档[ IETF RFC 2396 ]。 如果URI包含片段标识符(参见[ IETF RFC 2396 ]中的第4.1节),则该行为不是由本规范定义的,本规范的未来版本可能会定义行为。
Parameters | |
---|---|
uri |
String : The location of the XML document to be read. |
Returns | |
---|---|
Document |
If the LSParser is a synchronous LSParser , the newly created and populated Document is returned, or null if an error occured. If the LSParser is asynchronous, null is returned since the document object may not yet be constructed when this method returns. |
Throws | |
---|---|
DOMException |
INVALID_STATE_ERR: Raised if the LSParser.busy attribute is true . |
LSException |
PARSE_ERR: Raised if the LSParser was unable to load the XML document. DOM applications should attach a DOMErrorHandler using the parameter " error-handler" if they wish to get details on the error. |
Node parseWithContext (LSInput input, Node contextArg, short action)
从LSInput
标识的资源中解析XML片段,并将该内容插入到context
和action
参数指定位置的现有文档中。 在解析输入流时,上下文节点(或其父节点,取决于结果将被插入的位置)用于解析未绑定的名称空间前缀。 上下文节点的ownerDocument
节点(或节点自身,如果类型为DOCUMENT_NODE
的节点)用于解析默认属性和实体引用。
当新数据插入到文档中时,每个新的直接子节点或同级节点至少会触发一个突变事件。
如果上下文节点是Document
节点和行动是ACTION_REPLACE_CHILDREN
,然后就是作为上下文节点传递的文档将被改变,以使得其xmlEncoding
, documentURI
, xmlVersion
, inputEncoding
, xmlStandalone
,和所有其他的此类属性都设置为它们将被设置为如果使用LSParser.parse()
解析输入源。
即使LSParser
是异步的( LSParser.async
是true
),此方法始终是同步的。
如果在分析中发生错误,呼叫者通过通知ErrorHandler
与“相关实例error-handler所述的”参数DOMConfiguration
。
当调用parseWithContext
,以下配置参数的值将被忽略,并且将始终使用它们的默认值:“ validate ”,“ validate-if-schema ”和“ element-content-whitespace ”。 其他参数将被正常处理,解析器预计会调用LSParserFilter
就像整个文档被解析一样。
Parameters | |
---|---|
input |
LSInput : The LSInput from which the source document is to be read. The source document must be an XML fragment, i.e. anything except a complete XML document (except in the case where the context node of type DOCUMENT_NODE , and the action is ACTION_REPLACE_CHILDREN ), a DOCTYPE (internal subset), entity declaration(s), notation declaration(s), or XML or text declaration(s). |
contextArg |
Node : The node that is used as the context for the data that is being parsed. This node must be a Document node, a DocumentFragment node, or a node of a type that is allowed as a child of an Element node, e.g. it cannot be an Attribute node. |
action |
short : This parameter describes which action should be taken between the new set of nodes being inserted and the existing children of the context node. The set of possible actions is defined in ACTION_TYPES above. |
Returns | |
---|---|
Node |
Return the node that is the result of the parse operation. If the result is more than one top-level node, the first one is returned. |
Throws | |
---|---|
DOMException |
HIERARCHY_REQUEST_ERR: Raised if the content cannot replace, be inserted before, after, or as a child of the context node (see also Node.insertBefore or Node.replaceChild in [DOM Level 3 Core] ). NOT_SUPPORTED_ERR: Raised if the LSParser doesn't support this method, or if the context node is of type Document and the DOM implementation doesn't support the replacement of the DocumentType child or Element child. NO_MODIFICATION_ALLOWED_ERR: Raised if the context node is a read only node and the content is being appended to its child list, or if the parent node of the context node is read only node and the content is being inserted in its child list. INVALID_STATE_ERR: Raised if the LSParser.busy attribute is true . |
LSException |
PARSE_ERR: Raised if the LSParser was unable to load the XML fragment. DOM applications should attach a DOMErrorHandler using the parameter " error-handler" if they wish to get details on the error. |
void setFilter (LSParserFilter filter)
当提供过滤器时,实现会在构建DOM树结构时调用过滤器。 过滤器可以选择从正在构建的文档中移除元素,或者提前终止解析。
在应用了DOMConfiguration
参数所要求的操作后,将调用该过滤器。 例如,如果“ validate ”设置为true
,则在调用过滤器之前完成验证。
Parameters | |
---|---|
filter |
LSParserFilter
|