|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.xml.stream Interface XMLEventWriter
- All Superinterfaces:
- XMLEventConsumer
public interface XMLEventWriter
- extends XMLEventConsumer
Implements:
XMLEventConsumer
此类是用于编写 XML 文档的顶层接口。此接口的实例不需要验证 XML 的格式。
This is the top level interface for writing XML documents. Instances of this interface are not required to validate the form of the XML.
- Version:
- 1.0
- Author:
- Copyright (c) 2003 by BEA Systems. All Rights Reserved.
- See Also:
XMLEventReader
,XMLEvent
,Characters
,ProcessingInstruction
,StartElement
,EndElement
Method Summary | |
---|---|
void |
add(XMLEvent event)
Add an event to the output stream Adding a START_ELEMENT will open a new namespace scope that will be closed when the corresponding END_ELEMENT is written. |
void |
add(XMLEventReader reader)
Adds an entire stream to an output stream, calls next() on the inputStream argument until hasNext() returns false This should be treated as a convenience method that will perform the following loop over all the events in an event reader and call add on each event. |
void |
close()
Frees any resources associated with this stream |
void |
flush()
Writes any cached events to the underlying output mechanism |
NamespaceContext |
getNamespaceContext()
Returns the current namespace context. |
String |
getPrefix(String uri)
Gets the prefix the uri is bound to |
void |
setDefaultNamespace(String uri)
Binds a URI to the default namespace This URI is bound in the scope of the current START_ELEMENT / END_ELEMENT pair. |
void |
setNamespaceContext(NamespaceContext context)
Sets the current namespace context for prefix and uri bindings. |
void |
setPrefix(String prefix,
String uri)
Sets the prefix the uri is bound to. |
Method Detail |
---|
public void
flush() throws XMLStreamException
将所有缓存事件写入到底层输出机制。
Throws | XMLStreamException: |
flush
void flush() throws XMLStreamException
- Writes any cached events to the underlying output mechanism
- Throws:
XMLStreamException
public void
close() throws XMLStreamException
释放所有与此流关联的资源。
Throws | XMLStreamException: |
close
void close() throws XMLStreamException
- Frees any resources associated with this stream
- Throws:
XMLStreamException
public void
add(XMLEvent event) throws XMLStreamException
向输出流添加事件。添加 START_ELEMENT 将打开新的名称空间作用域,它将在写入相应的 END_ELEMENT 时被关闭。
添加到 writer 的事件的必需字段和可选字段 | |||
---|---|---|---|
事件类型 | 必需字段 | 可选字段 | 必需行为 |
START_ELEMENT | QName name | namespace、attribute | 通过用 XML 1.0 中对 START_ELEMENT 有效的语法写入事件的名称、名称空间和属性来编写 START_ELEMENT。名称是通过查找名称空间 URI 的前缀写入的。writer 可以配置为考虑 QName 的前缀。如果 writer 考虑前缀,则它必须使用在 QName 中设置的前缀。默认行为是在 EventWriter 的内部名称空间上下文中查找前缀的值。每个属性(如果有)使用在此表的属性部分所指定的行为写入。每个名称空间(如果有)使用在此表的名称空间部分所指定的行为写入。 |
END_ELEMENT | Qname name | 无 | 写入一个格式良好的 END_ELEMENT 标记。名称是通过查找名称空间 URI 的前缀写入的。writer 可以配置为考虑 QName 的前缀。如果 writer 考虑前缀,则它必须使用在 QName 中设置的前缀。默认行为是在 EventWriter 的内部名称空间上下文中查找前缀的值。如果 END_ELEMENT 名称和 START_ELEMENT 名称不匹配,则抛出 XMLStreamException。 |
ATTRIBUTE | QName name、String value | QName type | 使用同样的算法写入属性,以根据 START_ELEMENT 中使用的词法查找词法格式。默认行为是使用双引号将属性值括起来并转义出现在值中的所有双引号。类型值被忽略。 |
NAMESPACE | String prefix、String namespaceURI、boolean isDefaultNamespaceDeclaration | 无 | 写入一个名称空间声明。如果名称空间是默认名称空间声明(isDefault 为 true),则写入 xmlns="$namespaceURI",前缀是可选项。如果 isDefault 为 false,则必须声明前缀,writer 必须将 xmlns 附加为前缀并写出一个标准前缀声明。 |
PROCESSING_INSTRUCTION | 无 | String target、String data | 数据不需要附加前缀,可以为 null。目标是必需的,不可以为 null。writer 将直接在目标之后编写数据部分,并用合适的 XML 1.0 语法封闭起来 |
COMMENT | 无 | String comment | 如果存在注释(不为 null),则写入该注释,否则写入一个空注释 |
START_DOCUMENT | 无 | String encoding、boolean standalone、String version | 不需要将 START_DOCUMENT 事件写入流。如果存在,则属性以适当的 XML 声明语法写入 |
END_DOCUMENT | 无 | 无 | 不向输出写入任何内容 |
DTD | String DocumentTypeDefinition | 无 | DocumentTypeDefinition 被写入输出 |
event | 要被添加的事件 |
Throws | XMLStreamException: |
add
void add(XMLEvent event) throws XMLStreamException
- Add an event to the output stream
Adding a START_ELEMENT will open a new namespace scope that
will be closed when the corresponding END_ELEMENT is written.
Required and optional fields for events added to the writer Event Type Required Fields Optional Fields Required Behavior START_ELEMENT QName name namespaces , attributes A START_ELEMENT will be written by writing the name, namespaces, and attributes of the event in XML 1.0 valid syntax for START_ELEMENTs. The name is written by looking up the prefix for the namespace uri. The writer can be configured to respect prefixes of QNames. If the writer is respecting prefixes it must use the prefix set on the QName. The default behavior is to lookup the value for the prefix on the EventWriter's internal namespace context. Each attribute (if any) is written using the behavior specified in the attribute section of this table. Each namespace (if any) is written using the behavior specified in the namespace section of this table. END_ELEMENT Qname name None A well formed END_ELEMENT tag is written. The name is written by looking up the prefix for the namespace uri. The writer can be configured to respect prefixes of QNames. If the writer is respecting prefixes it must use the prefix set on the QName. The default behavior is to lookup the value for the prefix on the EventWriter's internal namespace context. If the END_ELEMENT name does not match the START_ELEMENT name an XMLStreamException is thrown. ATTRIBUTE QName name , String value QName type An attribute is written using the same algorithm to find the lexical form as used in START_ELEMENT. The default is to use double quotes to wrap attribute values and to escape any double quotes found in the value. The type value is ignored. NAMESPACE String prefix, String namespaceURI, boolean isDefaultNamespaceDeclaration None A namespace declaration is written. If the namespace is a default namespace declaration (isDefault is true) then xmlns="$namespaceURI" is written and the prefix is optional. If isDefault is false, the prefix must be declared and the writer must prepend xmlns to the prefix and write out a standard prefix declaration. PROCESSING_INSTRUCTION None String target, String data The data does not need to be present and may be null. Target is required and many not be null. The writer will write data section directly after the target, enclosed in appropriate XML 1.0 syntax COMMENT None String comment If the comment is present (not null) it is written, otherwise an an empty comment is written START_DOCUMENT None String encoding , boolean standalone, String version A START_DOCUMENT event is not required to be written to the stream. If present the attributes are written inside the appropriate XML declaration syntax END_DOCUMENT None None Nothing is written to the output DTD String DocumentTypeDefinition None The DocumentTypeDefinition is written to the output - Specified by:
add
in interfaceXMLEventConsumer
- Parameters:
event
- the event to be added- Throws:
XMLStreamException
public void
add(XMLEventReader reader) throws XMLStreamException
将整个流添加到输出流,在 hasNext() 返回 false 前对 inputStream 参数调用 next()。此方法应被视为一个便捷方法,它将在事件 reader 中的所有事件中执行以下循环,并对每个事件上调用 add。
reader | 要添加到输出的事件流 |
Throws | XMLStreamException: |
add
void add(XMLEventReader reader) throws XMLStreamException
- Adds an entire stream to an output stream,
calls next() on the inputStream argument until hasNext() returns false
This should be treated as a convenience method that will
perform the following loop over all the events in an
event reader and call add on each event.
- Parameters:
reader
- the event stream to add to the output- Throws:
XMLStreamException
public String
getPrefix(String uri) throws XMLStreamException
获取绑定 URI 的前缀
uri | 要查找的 URI |
Throws | XMLStreamException: |
getPrefix
String getPrefix(String uri) throws XMLStreamException
- Gets the prefix the uri is bound to
- Parameters:
uri
- the uri to look up- Throws:
XMLStreamException
public void
setPrefix(String prefix, String uri) throws XMLStreamException
设置绑定 URI 的前缀。前缀在当前 START_ELEMENT / END_ELEMENT 对的范围内绑定。如果在写入 START_ELEMENT 之前调用此方法,则前缀在根范围内绑定。
prefix | 要绑定到 URI 的前缀 |
uri | 要绑定到前缀的 URI |
Throws | XMLStreamException: |
setPrefix
void setPrefix(String prefix, String uri) throws XMLStreamException
- Sets the prefix the uri is bound to. This prefix is bound
in the scope of the current START_ELEMENT / END_ELEMENT pair.
If this method is called before a START_ELEMENT has been written
the prefix is bound in the root scope.
- Parameters:
prefix
- the prefix to bind to the uriuri
- the uri to bind to the prefix- Throws:
XMLStreamException
public void
setDefaultNamespace(String uri) throws XMLStreamException
将 URI 绑定到默认名称空间。此 URI 在当前 START_ELEMENT / END_ELEMENT 对的范围内绑定。如果在写入 START_ELEMENT 之前调用此方法,则 URI 在根范围内绑定。
uri | 要绑定到默认名称空间的 URI |
Throws | XMLStreamException: |
setDefaultNamespace
void setDefaultNamespace(String uri) throws XMLStreamException
- Binds a URI to the default namespace
This URI is bound
in the scope of the current START_ELEMENT / END_ELEMENT pair.
If this method is called before a START_ELEMENT has been written
the uri is bound in the root scope.
- Parameters:
uri
- the uri to bind to the default namespace- Throws:
XMLStreamException
public void
setNamespaceContext(javax.xml.namespace.NamespaceContext context) throws XMLStreamException
设置前缀的当前名称空间上下文和 URI 绑定。此上下文成为用于写入的根名称空间上下文,并将替换当前的根名称空间上下文。对 setPrefix 和 setDefaultNamespace 的后续调用将使用作为解析名称空间的根上下文传递到此方法的上下文来绑定名称空间。
context | 要用于此 writer 的名称空间上下文 |
Throws | XMLStreamException: |
setNamespaceContext
void setNamespaceContext(NamespaceContext context) throws XMLStreamException
- Sets the current namespace context for prefix and uri bindings.
This context becomes the root namespace context for writing and
will replace the current root namespace context. Subsequent calls
to setPrefix and setDefaultNamespace will bind namespaces using
the context passed to the method as the root context for resolving
namespaces.
- Parameters:
context
- the namespace context to use for this writer- Throws:
XMLStreamException
public javax.xml.namespace.NamespaceContext
getNamespaceContext()
返回当前名称空间上下文。
return | 当前名称空间上下文 |
getNamespaceContext
NamespaceContext getNamespaceContext()
- Returns the current namespace context.
- Returns:
- the current namespace context
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!