XMLStreamWriter (Java EE 5)

Java EE API


javax.xml.stream Interface XMLStreamWriter


public interface XMLStreamWriter


XMLStreamWriter 接口指定如何编写 XML。XMLStreamWriter 不在其输入中检查格式是否良好。但是,writeCharacters 方法需要转义属性值的 &、< 和 >。writeAttribute 方法将转义上述字符以及 ",以确保所有字符内容和属性值都是格式良好的。 必须单独编写每个 NAMESPACE 和 ATTRIBUTE。 如果 javax.xml.stream.isPrefixDefaulting 设置为 false,则在使用没有绑定到前缀的名称空间 URI 写入元素时将出现致命错误。 如果 javax.xml.stream.isPrefixDefaulting 设置为 true,则 XMLStreamWriter 实现必须为当前范围内遇到的每个未绑定 URI 写入前缀。
英文文档:

The XMLStreamWriter interface specifies how to write XML. The XMLStreamWriter does not perform well formedness checking on its input. However the writeCharacters method is required to escape & , < and > For attribute values the writeAttribute method will escape the above characters plus " to ensure that all character content and attribute values are well formed. Each NAMESPACE and ATTRIBUTE must be individually written. If javax.xml.stream.isPrefixDefaulting is set to false it is a fatal error if an element is written with namespace URI that has not been bound to a prefix. If javax.xml.stream.isPrefixDefaulting is set to true the XMLStreamWriter implementation must write a prefix for each unbound URI that it encounters in the current scope.

Version:
1.0
Author:
Copyright (c) 2003 by BEA Systems. All Rights Reserved.
See Also:
XMLOutputFactory, XMLStreamReader

Method Summary
 void
 void
 NamespaceContext
 String
 Object
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 

Method Detail

public void writeStartElement(String localName) throws XMLStreamException
向输出写入一个开始标记。所有 writeStartElement 方法都在内部名称空间上下文中打开一个新的作用域。编写相应的 EndElement 导致作用域被关闭。
localName 标记的本地名称,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeStartElement

void writeStartElement(String localName)
                       throws XMLStreamException
Writes a start tag to the output. All writeStartElement methods open a new scope in the internal namespace context. Writing the corresponding EndElement causes the scope to be closed.

Parameters:
localName - local name of the tag, may not be null
Throws:
XMLStreamException

public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException
向输出写入一个开始标记
namespaceURI 要使用的前缀的 namespaceURI ,不可以为 null
localName 标记的本地名称,不可以为 null
ThrowsXMLStreamException: 如果名称空间 URI 未绑定到前缀并且 javax.xml.stream.isPrefixDefaulting 未设置为 true
英文文档:

writeStartElement

void writeStartElement(String namespaceURI,
                       String localName)
                       throws XMLStreamException
Writes a start tag to the output

Parameters:
namespaceURI - the namespaceURI of the prefix to use, may not be null
localName - local name of the tag, may not be null
Throws:
XMLStreamException - if the namespace URI has not been bound to a prefix and javax.xml.stream.isPrefixDefaulting has not been set to true

public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException
向输出写入一个开始标记
localName 标记的本地名称,不可以为 null
prefix 标记的前缀,不可以为 null
namespaceURI 要绑定前缀的 URI,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeStartElement

void writeStartElement(String prefix,
                       String localName,
                       String namespaceURI)
                       throws XMLStreamException
Writes a start tag to the output

Parameters:
localName - local name of the tag, may not be null
prefix - the prefix of the tag, may not be null
namespaceURI - the uri to bind the prefix to, may not be null
Throws:
XMLStreamException

public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException
向输出写入一个空元素标记
namespaceURI 要绑定标记的 URI,不可以为 null
localName 标记的本地名称,不可以为 null
ThrowsXMLStreamException: 如果名称空间 URI 未绑定到前缀并且 javax.xml.stream.isPrefixDefaulting 未设置为 true
英文文档:

writeEmptyElement

void writeEmptyElement(String namespaceURI,
                       String localName)
                       throws XMLStreamException
Writes an empty element tag to the output

Parameters:
namespaceURI - the uri to bind the tag to, may not be null
localName - local name of the tag, may not be null
Throws:
XMLStreamException - if the namespace URI has not been bound to a prefix and javax.xml.stream.isPrefixDefaulting has not been set to true

public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException
向输出写入一个空元素标记
prefix 标记的前缀,不可以为 null
localName 标记的本地名称,不可以为 null
namespaceURI 要绑定标记的 URI,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeEmptyElement

void writeEmptyElement(String prefix,
                       String localName,
                       String namespaceURI)
                       throws XMLStreamException
Writes an empty element tag to the output

Parameters:
prefix - the prefix of the tag, may not be null
localName - local name of the tag, may not be null
namespaceURI - the uri to bind the tag to, may not be null
Throws:
XMLStreamException

public void writeEmptyElement(String localName) throws XMLStreamException
向输出写入一个空元素标记
localName 标记的本地名称,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeEmptyElement

void writeEmptyElement(String localName)
                       throws XMLStreamException
Writes an empty element tag to the output

Parameters:
localName - local name of the tag, may not be null
Throws:
XMLStreamException

public void writeEndElement() throws XMLStreamException
向依赖于 writer 内部状态的输出写入一个结束标记,以此确定事件的前缀和本地名称。
ThrowsXMLStreamException:
英文文档:

writeEndElement

void writeEndElement()
                     throws XMLStreamException
Writes an end tag to the output relying on the internal state of the writer to determine the prefix and local name of the event.

Throws:
XMLStreamException

public void writeEndDocument() throws XMLStreamException
关闭所有开始标记并写入相应的结束标记。
ThrowsXMLStreamException:
英文文档:

writeEndDocument

void writeEndDocument()
                      throws XMLStreamException
Closes any start tags and writes corresponding end tags.

Throws:
XMLStreamException

public void close() throws XMLStreamException
关闭此 writer 并释放与 writer 关联的所有资源。此方法不得关闭底层输出流。
ThrowsXMLStreamException:
英文文档:

close

void close()
           throws XMLStreamException
Close this writer and free any resources associated with the writer. This must not close the underlying output stream.

Throws:
XMLStreamException

public void flush() throws XMLStreamException
将所有缓存数据写入底层输出机制。
ThrowsXMLStreamException:
英文文档:

flush

void flush()
           throws XMLStreamException
Write any cached data to the underlying output mechanism.

Throws:
XMLStreamException

public void writeAttribute(String localName, String value) throws XMLStreamException
向输出流写入一个不带前缀的属性。
localName 属性的本地名称
value 属性的值
ThrowsIllegalStateException: 如果当前状态不允许 Attribute 写入
ThrowsXMLStreamException:
英文文档:

writeAttribute

void writeAttribute(String localName,
                    String value)
                    throws XMLStreamException
Writes an attribute to the output stream without a prefix.

Parameters:
localName - the local name of the attribute
value - the value of the attribute
Throws:
IllegalStateException - if the current state does not allow Attribute writing
XMLStreamException

public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException
向输出流写入一个属性
prefix 此属性的前缀
namespaceURI 此属性前缀的 URI
localName 属性的本地名称
value 属性的值
ThrowsIllegalStateException: 如果当前状态不允许 Attribute 写入
ThrowsXMLStreamException: 如果名称空间 URI 未绑定到前缀并且 javax.xml.stream.isPrefixDefaulting 未设置为 true
英文文档:

writeAttribute

void writeAttribute(String prefix,
                    String namespaceURI,
                    String localName,
                    String value)
                    throws XMLStreamException
Writes an attribute to the output stream

Parameters:
prefix - the prefix for this attribute
namespaceURI - the uri of the prefix for this attribute
localName - the local name of the attribute
value - the value of the attribute
Throws:
IllegalStateException - if the current state does not allow Attribute writing
XMLStreamException - if the namespace URI has not been bound to a prefix and javax.xml.stream.isPrefixDefaulting has not been set to true

public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException
向输出流写入一个属性
namespaceURI 此属性前缀的 URI
localName 属性的本地名称
value 属性的值
ThrowsIllegalStateException: 如果当前状态不允许 Attribute 写入
ThrowsXMLStreamException: 如果名称空间 URI 未绑定到前缀并且 javax.xml.stream.isPrefixDefaulting 未设置为 true
英文文档:

writeAttribute

void writeAttribute(String namespaceURI,
                    String localName,
                    String value)
                    throws XMLStreamException
Writes an attribute to the output stream

Parameters:
namespaceURI - the uri of the prefix for this attribute
localName - the local name of the attribute
value - the value of the attribute
Throws:
IllegalStateException - if the current state does not allow Attribute writing
XMLStreamException - if the namespace URI has not been bound to a prefix and javax.xml.stream.isPrefixDefaulting has not been set to true

public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException
向输出流写入一个名称空间。如果此方法的 prefix 参数为空字符串、"xmlns" 或 null,则此方法将委托给 writeDefaultNamespace。
prefix 要绑定名称空间的前缀
namespaceURI 要绑定前缀的 URI
ThrowsIllegalStateException: 如果当前状态不允许 Namespace 写入
ThrowsXMLStreamException:
英文文档:

writeNamespace

void writeNamespace(String prefix,
                    String namespaceURI)
                    throws XMLStreamException
Writes a namespace to the output stream If the prefix argument to this method is the empty string, "xmlns", or null this method will delegate to writeDefaultNamespace

Parameters:
prefix - the prefix to bind this namespace to
namespaceURI - the uri to bind the prefix to
Throws:
IllegalStateException - if the current state does not allow Namespace writing
XMLStreamException

public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException
向流写入默认名称空间
namespaceURI 要绑定默认名称空间的 URI
ThrowsIllegalStateException: 如果当前状态不允许 Namespace 写入
ThrowsXMLStreamException:
英文文档:

writeDefaultNamespace

void writeDefaultNamespace(String namespaceURI)
                           throws XMLStreamException
Writes the default namespace to the stream

Parameters:
namespaceURI - the uri to bind the default namespace to
Throws:
IllegalStateException - if the current state does not allow Namespace writing
XMLStreamException

public void writeComment(String data) throws XMLStreamException
编写一个包含数据的 XML 注释
data 包含在注释中的数据,可以为 null
ThrowsXMLStreamException:
英文文档:

writeComment

void writeComment(String data)
                  throws XMLStreamException
Writes an xml comment with the data enclosed

Parameters:
data - the data contained in the comment, may be null
Throws:
XMLStreamException

public void writeProcessingInstruction(String target) throws XMLStreamException
编写一条处理指令
target 处理指令的目标,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeProcessingInstruction

void writeProcessingInstruction(String target)
                                throws XMLStreamException
Writes a processing instruction

Parameters:
target - the target of the processing instruction, may not be null
Throws:
XMLStreamException

public void writeProcessingInstruction(String target, String data) throws XMLStreamException
编写一条处理指令
target 处理指令的目标,不可以为 null
data 处理指令中包含的数据,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeProcessingInstruction

void writeProcessingInstruction(String target,
                                String data)
                                throws XMLStreamException
Writes a processing instruction

Parameters:
target - the target of the processing instruction, may not be null
data - the data contained in the processing instruction, may not be null
Throws:
XMLStreamException

public void writeCData(String data) throws XMLStreamException
编写 CData 节
data CData 节中包含的数据,不可以为 null
ThrowsXMLStreamException:
英文文档:

writeCData

void writeCData(String data)
                throws XMLStreamException
Writes a CData section

Parameters:
data - the data contained in the CData Section, may not be null
Throws:
XMLStreamException

public void writeDTD(String dtd) throws XMLStreamException
编写 DTD 节。此字符串代表 XML 1.0 规范的整个 doctypedecl 产品。
dtd 要编写的 DTD
ThrowsXMLStreamException:
英文文档:

writeDTD

void writeDTD(String dtd)
              throws XMLStreamException
Write a DTD section. This string represents the entire doctypedecl production from the XML 1.0 specification.

Parameters:
dtd - the DTD to be written
Throws:
XMLStreamException

public void writeEntityRef(String name) throws XMLStreamException
编写一个实体引用
name 实体的名称
ThrowsXMLStreamException:
英文文档:

writeEntityRef

void writeEntityRef(String name)
                    throws XMLStreamException
Writes an entity reference

Parameters:
name - the name of the entity
Throws:
XMLStreamException

public void writeStartDocument() throws XMLStreamException
编写 XML 声明。默认 XML 版本为 1.0,默认编码为 utf-8
ThrowsXMLStreamException:
英文文档:

writeStartDocument

void writeStartDocument()
                        throws XMLStreamException
Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8

Throws:
XMLStreamException

public void writeStartDocument(String version) throws XMLStreamException
编写 XML 声明。默认 XML 版本为 1.0
version XML 文档的版本
ThrowsXMLStreamException:
英文文档:

writeStartDocument

void writeStartDocument(String version)
                        throws XMLStreamException
Write the XML Declaration. Defaults the XML version to 1.0

Parameters:
version - version of the xml document
Throws:
XMLStreamException

public void writeStartDocument(String encoding, String version) throws XMLStreamException
编写 XML 声明。注意,编码参数不能设置底层输出的实际编码。它必须在使用 XMLOutputFactory 创建 XMLStreamWriter 实例时进行设置
encoding XML 声明的编码
version XML 文档的版本
ThrowsXMLStreamException:
英文文档:

writeStartDocument

void writeStartDocument(String encoding,
                        String version)
                        throws XMLStreamException
Write the XML Declaration. Note that the encoding parameter does not set the actual encoding of the underlying output. That must be set when the instance of the XMLStreamWriter is created using the XMLOutputFactory

Parameters:
encoding - encoding of the xml declaration
version - version of the xml document
Throws:
XMLStreamException

public void writeCharacters(String text) throws XMLStreamException
向输出写入文本
text 要写入的值
ThrowsXMLStreamException:
英文文档:

writeCharacters

void writeCharacters(String text)
                     throws XMLStreamException
Write text to the output

Parameters:
text - the value to write
Throws:
XMLStreamException

public void writeCharacters(char[] text, int start, int len) throws XMLStreamException
向输出写入文本
text 要写入的值
start 数组中的起始位置
len 要写入的字符数
ThrowsXMLStreamException:
英文文档:

writeCharacters

void writeCharacters(char[] text,
                     int start,
                     int len)
                     throws XMLStreamException
Write text to the output

Parameters:
text - the value to write
start - the starting position in the array
len - the number of characters to write
Throws:
XMLStreamException

public String getPrefix(String uri) throws XMLStreamException
获取绑定 URI 的前缀
return 前缀或 null
ThrowsXMLStreamException:
英文文档:

getPrefix

String getPrefix(String uri)
                 throws XMLStreamException
Gets the prefix the uri is bound to

Returns:
the prefix or null
Throws:
XMLStreamException

public void setPrefix(String prefix, String uri) throws XMLStreamException
设置绑定 URI 的前缀。前缀在当前 START_ELEMENT / END_ELEMENT 对的范围内绑定。如果在写入 START_ELEMENT 之前调用此方法,则前缀在根范围内绑定。
prefix 要绑定到 URI 的前缀,不可以为 null
uri 要绑定到前缀的 URI,不可以为 null
ThrowsXMLStreamException:
英文文档:

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 uri, may not be null
uri - the uri to bind to the prefix, may be null
Throws:
XMLStreamException

public void setDefaultNamespace(String uri) throws XMLStreamException
将 URI 绑定到默认名称空间。此 URI 在当前 START_ELEMENT / END_ELEMENT 对的范围内绑定。如果在写入 START_ELEMENT 之前调用此方法,则 URI 在根范围内绑定。
uri 要绑定到默认名称空间的 URI,不可以为 null
ThrowsXMLStreamException:
英文文档:

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, may be null
Throws:
XMLStreamException

public void setNamespaceContext(javax.xml.namespace.NamespaceContext context) throws XMLStreamException
设置前缀和 URI 绑定的当前名称空间上下文。此上下文成为用于写入的根名称空间上下文,并将替换当前的根名称空间上下文。通过将传递到此方法的上下文用作解析名称空间的根上下文,对 setPrefix 和 setDefaultNamespace 的后续调用将绑定名称空间。此方法只能在文档开始处调用一次。这不会导致名称空间被声明。如果在名称空间上下文中存在前缀映射关系的名称空间 URI,则它将被视为已经声明,并且该前缀可以被 StreamWriter 使用。
context 将用于此 writer 的名称空间上下文,不可以为 null
ThrowsXMLStreamException:
英文文档:

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. This method may only be called once at the start of the document. It does not cause the namespaces to be declared. If a namespace URI to prefix mapping is found in the namespace context it is treated as declared and the prefix may be used by the StreamWriter.

Parameters:
context - the namespace context to use for this writer, may not be null
Throws:
XMLStreamException

public javax.xml.namespace.NamespaceContext getNamespaceContext()
返回当前名称空间上下文。
return 当前 NamespaceContext
英文文档:

getNamespaceContext

NamespaceContext getNamespaceContext()
Returns the current namespace context.

Returns:
the current NamespaceContext

public Object getProperty(String name) throws IllegalArgumentException
从底层实现获取功能/属性的值。
name 属性的名称,不可以为 null
return 属性的值
ThrowsIllegalArgumentException: 如果属性不受支持
ThrowsNullPointerException: 如果名称为 null。
英文文档:

getProperty

Object getProperty(String name)
                   throws IllegalArgumentException
Get the value of a feature/property from the underlying implementation

Parameters:
name - The name of the property, may not be null
Returns:
The value of the property
Throws:
IllegalArgumentException - if the property is not supported
NullPointerException - if the name is null


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!