|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.xml.stream Interface XMLStreamReader
- All Superinterfaces:
- XMLStreamConstants
- All Known Implementing Classes:
- StreamReaderDelegate
public interface XMLStreamReader
- extends XMLStreamConstants
XMLStreamReader 接口允许转发 XML 和对 XML 的只读访问。它被设计为读取 XML 数据的最低层且最有效的方法。
XMLStreamReader 被设计为使用 next() 和 hasNext() 在 XML 上进行迭代。可以使用诸如 getEventType()、getNamespaceURI()、getLocalName() 和 getText() 之类的方法访问数据:
next() 方法会使 reader 读取下一个解析事件。next() 方法返回一个整数,指示刚才读取的事件类型。
事件类型可以使用 getEventType() 来确定。
解析事件被定义为 XML 声明、DTD、开始标记、字符数据、空格、结束标记、注释或处理指令。属性或名称空间事件可以作为查询操作的结果在文档的根级别出现。
为了遵守 XML 1.0,XML 处理器必须将已声明的未解析实体的标识符、注释声明及其关联标识符传递给应用程序。此信息通过此接口上的属性 API 提供。以下两个属性允许访问此信息:javax.xml.stream.notations 和 javax.xml.stream.entities。当前事件为 DTD 时,以下调用将返回一个 Notation 列表 List l = (List) getProperty("javax.xml.stream.notations");
以下调用将返回一个实体声明列表:List l = (List) getProperty("javax.xml.stream.entities");
这些属性只能在 DTD 事件中访问,并且被定义为如果信息不可用,则返回 null。
下表描述了各种状态所对应的有效方法。如果某种方法是在无效状态中调用的,则该方法将抛出 java.lang.IllegalStateException。
每种状态对应的有效方法 | |||
---|---|---|---|
事件类型 | 有效方法 | ||
所有状态 | getProperty()、hasNext()、require()、close()、getNamespaceURI()、isStartElement()、isEndElement()、isCharacters()、isWhiteSpace()、getNamespaceContext()、getEventType()、getLocation()、hasText()、hasName() | ||
START_ELEMENT | next()、getName()、getLocalName()、hasName()、getPrefix()、getAttributeXXX()、isAttributeSpecified()、getNamespaceXXX()、getElementText()、nextTag() | ||
ATTRIBUTE | next()、nextTag()、getAttributeXXX()、isAttributeSpecified() | NAMESPACE | next()、nextTag()、getNamespaceXXX() |
END_ELEMENT | next()、getName()、getLocalName()、hasName()、getPrefix()、getNamespaceXXX()、nextTag() | ||
CHARACTERS | next()、getTextXXX()、nextTag() | ||
CDATA | next()、getTextXXX()、nextTag() | ||
COMMENT | next()、getTextXXX()、nextTag() | ||
SPACE | next()、getTextXXX()、nextTag() | ||
START_DOCUMENT | next()、getEncoding()、getVersion()、isStandalone()、standaloneSet()、getCharacterEncodingScheme()、nextTag() | ||
END_DOCUMENT | close() | ||
PROCESSING_INSTRUCTION | next()、getPITarget()、getPIData()、nextTag() | ||
ENTITY_REFERENCE | next()、getLocalName()、getText()、nextTag() | ||
DTD | next()、getText()、nextTag() |
version | 1.0 |
See also | javax.xml.stream.events.XMLEvent, javax.xml.stream.XMLInputFactory, javax.xml.stream.XMLStreamWriter |
The XMLStreamReader interface allows forward, read-only access to XML. It is designed to be the lowest level and most efficient way to read XML data.
The XMLStreamReader is designed to iterate over XML using next() and hasNext(). The data can be accessed using methods such as getEventType(), getNamespaceURI(), getLocalName() and getText();
The next() method causes the reader to read the next parse event. The next() method returns an integer which identifies the type of event just read.
The event type can be determined using getEventType().
Parsing events are defined as the XML Declaration, a DTD, start tag, character data, white space, end tag, comment, or processing instruction. An attribute or namespace event may be encountered at the root level of a document as the result of a query operation.
For XML 1.0 compliance an XML processor must pass the
identifiers of declared unparsed entities, notation declarations and their
associated identifiers to the application. This information is
provided through the property API on this interface.
The following two properties allow access to this information:
javax.xml.stream.notations and javax.xml.stream.entities.
When the current event is a DTD the following call will return a
list of Notations
List l = (List) getProperty("javax.xml.stream.notations");
The following call will return a list of entity declarations:
List l = (List) getProperty("javax.xml.stream.entities");
These properties can only be accessed during a DTD event and
are defined to return null if the information is not available.
The following table describes which methods are valid in what state. If a method is called in an invalid state the method will throw a java.lang.IllegalStateException.
Valid methods for each state | |||
---|---|---|---|
Event Type | Valid Methods | ||
All States | getProperty(), hasNext(), require(), close(), getNamespaceURI(), isStartElement(), isEndElement(), isCharacters(), isWhiteSpace(), getNamespaceContext(), getEventType(),getLocation(), hasText(), hasName() | ||
START_ELEMENT | next(), getName(), getLocalName(), hasName(), getPrefix(), getAttributeXXX(), isAttributeSpecified(), getNamespaceXXX(), getElementText(), nextTag() | ||
ATTRIBUTE | next(), nextTag() getAttributeXXX(), isAttributeSpecified(), | NAMESPACE | next(), nextTag() getNamespaceXXX() |
END_ELEMENT | next(), getName(), getLocalName(), hasName(), getPrefix(), getNamespaceXXX(), nextTag() | ||
CHARACTERS | next(), getTextXXX(), nextTag() | ||
CDATA | next(), getTextXXX(), nextTag() | ||
COMMENT | next(), getTextXXX(), nextTag() | ||
SPACE | next(), getTextXXX(), nextTag() | ||
START_DOCUMENT | next(), getEncoding(), getVersion(), isStandalone(), standaloneSet(), getCharacterEncodingScheme(), nextTag() | ||
END_DOCUMENT | close() | ||
PROCESSING_INSTRUCTION | next(), getPITarget(), getPIData(), nextTag() | ||
ENTITY_REFERENCE | next(), getLocalName(), getText(), nextTag() | ||
DTD | next(), getText(), nextTag() |
- Version:
- 1.0
- Author:
- Copyright (c) 2003 by BEA Systems. All Rights Reserved.
- See Also:
XMLEvent
,XMLInputFactory
,XMLStreamWriter
Field Summary |
---|
Fields inherited from interface javax.xml.stream.XMLStreamConstants |
---|
ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT |
Method Summary | |
---|---|
void |
close()
Frees any resources associated with this Reader. |
int |
getAttributeCount()
Returns the count of attributes on this START_ELEMENT, this method is only valid on a START_ELEMENT or ATTRIBUTE. |
String |
getAttributeLocalName(int index)
Returns the localName of the attribute at the provided index |
QName |
getAttributeName(int index)
Returns the qname of the attribute at the provided index |
String |
getAttributeNamespace(int index)
Returns the namespace of the attribute at the provided index |
String |
getAttributePrefix(int index)
Returns the prefix of this attribute at the provided index |
String |
getAttributeType(int index)
Returns the XML type of the attribute at the provided index |
String |
getAttributeValue(int index)
Returns the value of the attribute at the index |
String |
getAttributeValue(String namespaceURI,
String localName)
Returns the normalized attribute value of the attribute with the namespace and localName If the namespaceURI is null the namespace is not checked for equality |
String |
getCharacterEncodingScheme()
Returns the character encoding declared on the xml declaration Returns null if none was declared |
String |
getElementText()
Reads the content of a text-only element, an exception is thrown if this is not a text-only element. |
String |
getEncoding()
Return input encoding if known or null if unknown. |
int |
getEventType()
Returns an integer code that indicates the type of the event the cursor is pointing to. |
String |
getLocalName()
Returns the (local) name of the current event. |
Location |
getLocation()
Return the current location of the processor. |
QName |
getName()
Returns a QName for the current START_ELEMENT or END_ELEMENT event |
NamespaceContext |
getNamespaceContext()
Returns a read only namespace context for the current position. |
int |
getNamespaceCount()
Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT, this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. |
String |
getNamespacePrefix(int index)
Returns the prefix for the namespace declared at the index. |
String |
getNamespaceURI()
If the current event is a START_ELEMENT or END_ELEMENT this method returns the URI of the prefix or the default namespace. |
String |
getNamespaceURI(int index)
Returns the uri for the namespace declared at the index. |
String |
getNamespaceURI(String prefix)
Return the uri for the given prefix. |
String |
getPIData()
Get the data section of a processing instruction |
String |
getPITarget()
Get the target of a processing instruction |
String |
getPrefix()
Returns the prefix of the current event or null if the event does not have a prefix |
Object |
getProperty(String name)
Get the value of a feature/property from the underlying implementation |
String |
getText()
Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD. |
char[] |
getTextCharacters()
Returns an array which contains the characters from this event. |
int |
getTextCharacters(int sourceStart,
char[] target,
int targetStart,
int length)
Gets the the text associated with a CHARACTERS, SPACE or CDATA event. |
int |
getTextLength()
Returns the length of the sequence of characters for this Text event within the text character array. |
int |
getTextStart()
Returns the offset into the text character array where the first character (of this text event) is stored. |
String |
getVersion()
Get the xml version declared on the xml declaration Returns null if none was declared |
boolean |
hasName()
returns true if the current event has a name (is a START_ELEMENT or END_ELEMENT) returns false otherwise |
boolean |
hasNext()
Returns true if there are more parsing events and false if there are no more events. |
boolean |
hasText()
Return true if the current event has text, false otherwise The following events have text: CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT, SPACE |
boolean |
isAttributeSpecified(int index)
Returns a boolean which indicates if this attribute was created by default |
boolean |
isCharacters()
Returns true if the cursor points to a character data event |
boolean |
isEndElement()
Returns true if the cursor points to an end tag (otherwise false) |
boolean |
isStandalone()
Get the standalone declaration from the xml declaration |
boolean |
isStartElement()
Returns true if the cursor points to a start tag (otherwise false) |
boolean |
isWhiteSpace()
Returns true if the cursor points to a character data event that consists of all whitespace |
int |
next()
Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks. |
int |
nextTag()
Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached. |
void |
require(int type,
String namespaceURI,
String localName)
Test if the current event is of the given type and if the namespace and name match the current namespace and name of the current event. |
boolean |
standaloneSet()
Checks if standalone was set in the document |
Method Detail |
---|
public Object
getProperty(String name) throws IllegalArgumentException
从底层实现获取功能/属性的值。
name | 属性的名称,不可以为 null |
return | 属性的值 |
Throws | IllegalArgumentException: 如果 name 为 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 name is null
public int
next() throws XMLStreamException
获取下一个解析事件——处理器可以用单个存储块返回所有连续的字符数据,它也可以将其分割成几个存储块。如果属性 javax.xml.stream.isCoalescing 设置为 true,则元素内容必须组合,对于连续元素内容或 CDATA 节,只需要返回一个 CHARACTERS 事件。
默认情况下,必须扩展实体引用,并透明地报告给应用程序。如果实体引用不能扩展,则抛出异常。如果元素内容为空(即内容为 ""),则不报告 CHARACTERS 事件。
假设给定以下 XML:
<foo><!--description-->内容文本<![CDATA[<greeting>Hello</greeting>]]>其他内容</foo>
对 foo 调用 next() 的行为将是:
1- 注释 (COMMENT)
2- 然后是字符节 (CHARACTERS)
3- 接下来是 CDATA 节(另一个 CHARACTERS)
4- 再接下来是下一个字符节 (另一个 CHARACTERS)
5- 最后是 END_ELEMENT
注: 空元素(如 <tag/>)将用以下两个单独的事件报告:START_ELEMENT 和 END_ELEMENT——这保证了空元素对 <tag></tag> 解析的等效性。 如果是在 hasNext() 返回 false 之后调用此方法,则此方法将抛出 IllegalStateException。
return | 与当前解析事件相对应的整数码 |
Throws | NoSuchElementException: 如果在 hasNext() 返回 false 时调用 |
Throws | XMLStreamException: 如果处理底层 XML 源时发生错误 |
See also | javax.xml.stream.events.XMLEvent |
next
int next() throws XMLStreamException
- Get next parsing event - a processor may return all contiguous
character data in a single chunk, or it may split it into several chunks.
If the property javax.xml.stream.isCoalescing is set to true
element content must be coalesced and only one CHARACTERS event
must be returned for contiguous element content or
CDATA Sections.
By default entity references must be
expanded and reported transparently to the application.
An exception will be thrown if an entity reference cannot be expanded.
If element content is empty (i.e. content is "") then no CHARACTERS event will be reported.
Given the following XML:
<foo><!--description-->content text<![CDATA[<greeting>Hello</greeting>]]>other content</foo>
The behavior of calling next() when being on foo will be:
1- the comment (COMMENT)
2- then the characters section (CHARACTERS)
3- then the CDATA section (another CHARACTERS)
4- then the next characters section (another CHARACTERS)
5- then the END_ELEMENT
NOTE: empty element (such as <tag/>) will be reported with two separate events: START_ELEMENT, END_ELEMENT - This preserves parsing equivalency of empty element to <tag></tag>. This method will throw an IllegalStateException if it is called after hasNext() returns false.
- Returns:
- the integer code corresponding to the current parse event
- Throws:
NoSuchElementException
- if this is called when hasNext() returns falseXMLStreamException
- if there is an error processing the underlying XML source- See Also:
XMLEvent
public void
require(int type, String namespaceURI, String localName) throws XMLStreamException
测试当前事件是否属于给定的类型,以及名称空间和名称是否与当前事件的当前名称空间和名称相匹配。如果 namespaceURI 为 null,则该参数不进行相等性检查;如果 localName 为 null,则该参数不进行相等性检查。
type | 事件类型 |
namespaceURI | 事件的 URI,可以为 null |
localName | 事件的 localName,可以为 null |
Throws | XMLStreamException: 如果所需值不匹配。 |
require
void require(int type, String namespaceURI, String localName) throws XMLStreamException
- Test if the current event is of the given type and if the namespace and name match the current
namespace and name of the current event. If the namespaceURI is null it is not checked for equality,
if the localName is null it is not checked for equality.
- Parameters:
type
- the event typenamespaceURI
- the uri of the event, may be nulllocalName
- the localName of the event, may be null- Throws:
XMLStreamException
- if the required values are not matched.
public String
getElementText() throws XMLStreamException
读取纯文本元素的内容,如果不是纯文本元素,则抛出异常。无论 javax.xml.stream.isCoalescing 的值如何,此方法始终返回组合的内容。
前置条件:当前事件是 START_ELEMENT。
后置条件:当前事件是相应的 END_ELEMENT。
此方法执行下列操作(实现可以随意优化,但必须执行等效处理):
if(getEventType() != XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"parser must be on START_ELEMENT to read next text", getLocation());
}
int eventType = next();
StringBuffer content = new StringBuffer();
while(eventType != XMLStreamConstants.END_ELEMENT ) {
if(eventType == XMLStreamConstants.CHARACTERS
|| eventType == XMLStreamConstants.CDATA
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.ENTITY_REFERENCE) {
buf.append(getText());
} else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT) {
// skipping
} else if(eventType == XMLStreamConstants.END_DOCUMENT) {
throw new XMLStreamException(
"unexpected end of document when reading element text content", this);
} else if(eventType == XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"element text content may not contain START_ELEMENT", getLocation());
} else {
throw new XMLStreamException(
"Unexpected event type "+eventType, getLocation());
}
eventType = next();
}
return buf.toString();
Throws | XMLStreamException: 如果当前事件不是 START_ELEMENT 或者遇到了非文本元素 |
getElementText
String getElementText() throws XMLStreamException
- Reads the content of a text-only element, an exception is thrown if this is
not a text-only element.
Regardless of value of javax.xml.stream.isCoalescing this method always returns coalesced content.
Precondition: the current event is START_ELEMENT.
Postcondition: the current event is the corresponding END_ELEMENT.
The method does the following (implementations are free to optimized but must do equivalent processing):if(getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", getLocation()); } int eventType = next(); StringBuffer content = new StringBuffer(); while(eventType != XMLStreamConstants.END_ELEMENT ) { if(eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) { buf.append(getText()); } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { // skipping } else if(eventType == XMLStreamConstants.END_DOCUMENT) { throw new XMLStreamException( "unexpected end of document when reading element text content", this); } else if(eventType == XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException( "element text content may not contain START_ELEMENT", getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+eventType, getLocation()); } eventType = next(); } return buf.toString();
- Throws:
XMLStreamException
- if the current event is not a START_ELEMENT or if a non text element is encountered
public int
nextTag() throws XMLStreamException
在到达 START_ELEMENT 或 END_ELEMENT 之前,跳过所有空格(isWhiteSpace() 返回 true)、COMMENT 或 PROCESSING_INSTRUCTION。如果遇到空格字符、COMMENT、PROCESSING_INSTRUCTION、START_ELEMENT、END_ELEMENT 以外的其他内容,则抛出异常。在处理以空格分隔的纯元素内容时,应使用此方法。
前置条件:无
后置条件:当前事件为 START_ELEMENT 或 END_ELEMENT,并且光标已经移到任何空格事件上。
它实际上执行下列操作(实现可以随意优化,但必须执行等效处理):
int eventType = next();
while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
// skip whitespace
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT
) {
eventType = next();
}
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
throw new String XMLStreamException("expected start or end tag", getLocation());
}
return eventType;
return | 元素读取的事件类型(START_ELEMENT 或 END_ELEMENT) |
Throws | XMLStreamException: 如果当前事件不是空格、PROCESSING_INSTRUCTION、START_ELEMENT 或 END_ELEMENT |
Throws | NoSuchElementException: 如果在 hasNext() 返回 false 时调用 |
nextTag
int nextTag() throws XMLStreamException
- Skips any white space (isWhiteSpace() returns true), COMMENT,
or PROCESSING_INSTRUCTION,
until a START_ELEMENT or END_ELEMENT is reached.
If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT
are encountered, an exception is thrown. This method should
be used when processing element-only content seperated by white space.
Precondition: none
Postcondition: the current event is START_ELEMENT or END_ELEMENT and cursor may have moved over any whitespace event.
Essentially it does the following (implementations are free to optimized but must do equivalent processing):int eventType = next(); while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT ) { eventType = next(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new String XMLStreamException("expected start or end tag", getLocation()); } return eventType;
- Returns:
- the event type of the element read (START_ELEMENT or END_ELEMENT)
- Throws:
XMLStreamException
- if the current event is not white space, PROCESSING_INSTRUCTION, START_ELEMENT or END_ELEMENTNoSuchElementException
- if this is called when hasNext() returns false
public boolean
hasNext() throws XMLStreamException
如果有多个解析事件,则返回 true,如果不再有事件,则返回 false。如果 XMLStreamReader 的当前状态为 END_DOCUMENT,则此方法返回 false
return | 如果有多个事件,则返回 true,否则返回 false |
Throws | XMLStreamException: 如果检测下一个状态时发生严重错误 |
hasNext
boolean hasNext() throws XMLStreamException
- Returns true if there are more parsing events and false
if there are no more events. This method will return
false if the current state of the XMLStreamReader is
END_DOCUMENT
- Returns:
- true if there are more events, false otherwise
- Throws:
XMLStreamException
- if there is a fatal error detecting the next state
public void
close() throws XMLStreamException
释放与此 Reader 关联的所有资源。此方法不会关闭底层输入源。
Throws | XMLStreamException: 如果释放关联资源时发生错误 |
close
void close() throws XMLStreamException
- Frees any resources associated with this Reader. This method does not close the
underlying input source.
- Throws:
XMLStreamException
- if there are errors freeing associated resources
public String
getNamespaceURI(String prefix)
返回给定前缀的 URI。返回的 URI 取决于处理器的当前状态。
注:根据 Namespaces in XML 规范中的定义,'xml' 前缀被绑定到 "http://www.w3.org/XML/1998/namespace"。
注:'xmlns' 前缀必须被解析到以下名称空间:http://www.w3.org/2000/xmlns/
prefix | 要查找的前缀,不可以为 null |
return | 绑定到给定前缀的 URI,如果未绑定,则返回 null |
Throws | IllegalArgumentException: 如果前缀为 null |
getNamespaceURI
String getNamespaceURI(String prefix)
- Return the uri for the given prefix.
The uri returned depends on the current state of the processor.
NOTE:The 'xml' prefix is bound as defined in Namespaces in XML specification to "http://www.w3.org/XML/1998/namespace".
NOTE: The 'xmlns' prefix must be resolved to following namespace http://www.w3.org/2000/xmlns/
- Parameters:
prefix
- The prefix to lookup, may not be null- Returns:
- the uri bound to the given prefix or null if it is not bound
- Throws:
IllegalArgumentException
- if the prefix is null
public boolean
isStartElement()
如果光标指向开始标记,则返回 true(否则返回 false)
return | 如果光标指向开始标记,则返回 true,否则返回 false |
isStartElement
boolean isStartElement()
- Returns true if the cursor points to a start tag (otherwise false)
- Returns:
- true if the cursor points to a start tag, false otherwise
public boolean
isEndElement()
如果光标指向结束标记,则返回 true(否则返回 false)
return | 如果光标指向结束标记,则返回 true,否则返回 false |
isEndElement
boolean isEndElement()
- Returns true if the cursor points to an end tag (otherwise false)
- Returns:
- true if the cursor points to an end tag, false otherwise
public boolean
isCharacters()
如果光标指向字符数据事件,则返回 true
return | 如果光标指向字符数据,则返回 true,否则返回 false |
isCharacters
boolean isCharacters()
- Returns true if the cursor points to a character data event
- Returns:
- true if the cursor points to character data, false otherwise
public boolean
isWhiteSpace()
如果光标指向由所有空格组成的字符数据事件,则返回 true
return | 如果光标指向所有空格,则返回 true,否则返回 false |
isWhiteSpace
boolean isWhiteSpace()
- Returns true if the cursor points to a character data event
that consists of all whitespace
- Returns:
- true if the cursor points to all whitespace, false otherwise
public String
getAttributeValue(String namespaceURI, String localName)
返回带有名称空间和 localName 的属性的规范化属性值。如果 namespaceURI 为 null,则名称空间不进行相等性检查
namespaceURI | 属性的名称空间 |
localName | 属性的本地名称,不可以为 null |
return | 返回属性的值,如果未找到,则返回 null |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeValue
String getAttributeValue(String namespaceURI, String localName)
- Returns the normalized attribute value of the
attribute with the namespace and localName
If the namespaceURI is null the namespace
is not checked for equality
- Parameters:
namespaceURI
- the namespace of the attributelocalName
- the local name of the attribute, cannot be null- Returns:
- returns the value of the attribute , returns null if not found
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public int
getAttributeCount()
返回此 START_ELEMENT 中的属性计数,此方法仅对 START_ELEMENT 或 ATTRIBUTE 有效。此计数不包括名称空间定义。属性索引从零开始。
return | 返回属性数 |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeCount
int getAttributeCount()
- Returns the count of attributes on this START_ELEMENT,
this method is only valid on a START_ELEMENT or ATTRIBUTE. This
count excludes namespace definitions. Attribute indices are
zero-based.
- Returns:
- returns the number of attributes
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public javax.xml.namespace.QName
getAttributeName(int index)
返回所提供索引处的属性的 qname
index | 属性的位置 |
return | 属性的 QName |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeName
QName getAttributeName(int index)
- Returns the qname of the attribute at the provided index
- Parameters:
index
- the position of the attribute- Returns:
- the QName of the attribute
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public String
getAttributeNamespace(int index)
返回所提供索引处的属性的名称空间
index | 属性的位置 |
return | 名称空间 URI(可以为 null) |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeNamespace
String getAttributeNamespace(int index)
- Returns the namespace of the attribute at the provided
index
- Parameters:
index
- the position of the attribute- Returns:
- the namespace URI (can be null)
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public String
getAttributeLocalName(int index)
返回所提供索引处的属性的 localName
index | 属性的位置 |
return | 属性的 localName |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeLocalName
String getAttributeLocalName(int index)
- Returns the localName of the attribute at the provided
index
- Parameters:
index
- the position of the attribute- Returns:
- the localName of the attribute
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public String
getAttributePrefix(int index)
返回所提供索引处的属性的前缀
index | 属性的位置 |
return | 属性的前缀 |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributePrefix
String getAttributePrefix(int index)
- Returns the prefix of this attribute at the
provided index
- Parameters:
index
- the position of the attribute- Returns:
- the prefix of the attribute
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public String
getAttributeType(int index)
返回所提供索引处的属性的 XML 类型
index | 属性的位置 |
return | 属性的 XML 类型 |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeType
String getAttributeType(int index)
- Returns the XML type of the attribute at the provided
index
- Parameters:
index
- the position of the attribute- Returns:
- the XML type of the attribute
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public String
getAttributeValue(int index)
返回 index 处的属性值
index | 属性的位置 |
return | 属性值 |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
getAttributeValue
String getAttributeValue(int index)
- Returns the value of the attribute at the
index
- Parameters:
index
- the position of the attribute- Returns:
- the attribute value
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public boolean
isAttributeSpecified(int index)
返回一个 boolean 值,它指示此属性是否为默认创建的
index | 属性的位置 |
return | 如果这是一个默认属性,则返回 true |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 ATTRIBUTE |
isAttributeSpecified
boolean isAttributeSpecified(int index)
- Returns a boolean which indicates if this
attribute was created by default
- Parameters:
index
- the position of the attribute- Returns:
- true if this is a default attribute
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or ATTRIBUTE
public int
getNamespaceCount()
返回在 START_ELEMENT 或 END_ELEMENT 中声明的名称空间的计数,此方法仅对 START_ELEMENT、END_ELEMENT 或 NAMESPACE 有效。对于 END_ELEMENT,计数是最大的名称空间范围。它等同于 SAX 回调所报告的有关结束元素事件的信息。
return | 返回此特定元素中的名称空间声明数 |
Throws | IllegalStateException: 如果不是 START_ELEMENT、END_ELEMENT 或 NAMESPACE |
getNamespaceCount
int getNamespaceCount()
- Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT,
this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. On
an END_ELEMENT the count is of the namespaces that are about to go
out of scope. This is the equivalent of the information reported
by SAX callback for an end element event.
- Returns:
- returns the number of namespace declarations on this specific element
- Throws:
IllegalStateException
- if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
public String
getNamespacePrefix(int index)
返回在 index 处声明的名称空间前缀。如果是默认名称空间声明,则返回 null
index | 名称空间声明的位置 |
return | 返回名称空间前缀 |
Throws | IllegalStateException: 如果不是 START_ELEMENT、END_ELEMENT 或 NAMESPACE |
getNamespacePrefix
String getNamespacePrefix(int index)
- Returns the prefix for the namespace declared at the
index. Returns null if this is the default namespace
declaration
- Parameters:
index
- the position of the namespace declaration- Returns:
- returns the namespace prefix
- Throws:
IllegalStateException
- if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
public String
getNamespaceURI(int index)
返回在 index 处声明的名称空间 URI。
index | 名称空间声明的位置 |
return | 返回名称空间 URI |
Throws | IllegalStateException: 如果不是 START_ELEMENT、END_ELEMENT 或 NAMESPACE |
getNamespaceURI
String getNamespaceURI(int index)
- Returns the uri for the namespace declared at the
index.
- Parameters:
index
- the position of the namespace declaration- Returns:
- returns the namespace uri
- Throws:
IllegalStateException
- if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
public javax.xml.namespace.NamespaceContext
getNamespaceContext()
返回当前位置的只读名称空间上下文。上下文为 transient,只在调用 next() 更改 reader 的状态之前才有效。
return | 返回名称空间上下文 |
getNamespaceContext
NamespaceContext getNamespaceContext()
- Returns a read only namespace context for the current
position. The context is transient and only valid until
a call to next() changes the state of the reader.
- Returns:
- return a namespace context
public int
getEventType()
返回一个整数码,指示光标所指向的事件的类型。
英文文档:
getEventType
int getEventType()
- Returns an integer code that indicates the type
of the event the cursor is pointing to.
public String
getText()
以字符串的形式返回解析事件的当前值,此方法返回 CHARACTERS 事件的字符串值,返回 COMMENT 的值、ENTITY_REFERENCE 的替代值、CDATA 节的字符串值、SPACE 事件的字符串值、DTD 内部子集的字符串值。如果已经解析了 ENTITY_REFERENCE,则所有字符数据都将被报告为 CHARACTERS 事件。
return | 当前文本或 null |
Throws | IllegalStateException: 如果此状态不是有效的文本状态。 |
getText
String getText()
- Returns the current value of the parse event as a string,
this returns the string value of a CHARACTERS event,
returns the value of a COMMENT, the replacement value
for an ENTITY_REFERENCE, the string value of a CDATA section,
the string value for a SPACE event,
or the String value of the internal subset of the DTD.
If an ENTITY_REFERENCE has been resolved, any character data
will be reported as CHARACTERS events.
- Returns:
- the current text or null
- Throws:
IllegalStateException
- if this state is not a valid text state.
public char[]
getTextCharacters()
返回一个包含此事件中字符的数组。此数组应被视为只读的和 transient,即在 XMLStreamReader 移动到下一个事件之前,数组将包含文本字符。试图在超出该时间范围后保存到字符数组,或者试图修改数组的内容,这些都是违背此接口约定的行为。
return | 当前文本或空数组 |
Throws | IllegalStateException: 如果此状态不是有效的文本状态。 |
getTextCharacters
char[] getTextCharacters()
- Returns an array which contains the characters from this event.
This array should be treated as read-only and transient. I.e. the array will
contain the text characters until the XMLStreamReader moves on to the next event.
Attempts to hold onto the character array beyond that time or modify the
contents of the array are breaches of the contract for this interface.
- Returns:
- the current text or an empty array
- Throws:
IllegalStateException
- if this state is not a valid text state.
public int
getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException
获取与 CHARACTERS、SPACE 或 CDATA 事件关联的文本。以 "sourceStart" 开始的文本被复制到以 "targetStart" 开始的 "target" 中。最多复制 "length" 个字符。返回实际复制的字符数。
"sourceStart" 参数必须大于等于 0 且小于等于与事件关联的字符数。通常,一个请求文本以 "sourceStart" 为 0 开始。如果实际复制的字符数小于 "length",则表示没有更多的文本。否则,在检索完所有文本之前,需要进行后续的调用。例如:
int length = 1024; char[] myBuffer = new char[ length ];
for ( int sourceStart = 0 ; ; sourceStart += length ) { int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length );
if (nCopied < length) break; }
如果在底层源中存在任何 XML 错误,则抛出 XMLStreamException。"targetStart" 参数必须大于等于 0 且小于 "target" 的长度,Length 必须大于 0,而 "targetStart + length" 必须小于等于 "target" 的长度。
sourceStart | 要复制的源数组中第一个字符的索引 |
target | 目标数组 |
targetStart | 目标数组中的起始偏移量 |
length | 要复制的字符数 |
return | 实际复制的字符数 |
Throws | XMLStreamException: 如果底层 XML 源不是格式良好的 |
Throws | IndexOutOfBoundsException: 如果 targetStart < 0 或 > 目标长度 |
Throws | IndexOutOfBoundsException: 如果 length < 0 或 targetStart + length > 目标长度 |
Throws | UnsupportedOperationException: 如果此方法不受支持 |
Throws | NullPointerException: 如果目标为 null |
getTextCharacters
int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException
- Gets the the text associated with a CHARACTERS, SPACE or CDATA event.
Text starting a "sourceStart" is copied into "target" starting at "targetStart".
Up to "length" characters are copied. The number of characters actually copied is returned.
The "sourceStart" argument must be greater or equal to 0 and less than or equal to
the number of characters associated with the event. Usually, one requests text starting at a "sourceStart" of 0.
If the number of characters actually copied is less than the "length", then there is no more text.
Otherwise, subsequent calls need to be made until all text has been retrieved. For example:
int length = 1024; char[] myBuffer = new char[ length ]; for ( int sourceStart = 0 ; ; sourceStart += length ) { int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length ); if (nCopied < length) break; }
XMLStreamException may be thrown if there are any XML errors in the underlying source. The "targetStart" argument must be greater than or equal to 0 and less than the length of "target", Length must be greater than 0 and "targetStart + length" must be less than or equal to length of "target". - Parameters:
sourceStart
- the index of the first character in the source array to copytarget
- the destination arraytargetStart
- the start offset in the target arraylength
- the number of characters to copy- Returns:
- the number of characters actually copied
- Throws:
XMLStreamException
- if the underlying XML source is not well-formedIndexOutOfBoundsException
- if targetStart < 0 or > than the length of targetIndexOutOfBoundsException
- if length < 0 or targetStart + length > length of targetUnsupportedOperationException
- if this method is not supportedNullPointerException
- is if target is null
public int
getTextStart()
返回存储(此文本事件的)第一个字符位置处的文本字符数组的偏移量。
Throws | IllegalStateException: 如果此状态不是有效的文本状态。 |
getTextStart
int getTextStart()
- Returns the offset into the text character array where the first
character (of this text event) is stored.
- Throws:
IllegalStateException
- if this state is not a valid text state.
public int
getTextLength()
返回文本字符数组中此文本事件的字符序列长度。
Throws | IllegalStateException: 如果此状态不是有效的文本状态。 |
getTextLength
int getTextLength()
- Returns the length of the sequence of characters for this
Text event within the text character array.
- Throws:
IllegalStateException
- if this state is not a valid text state.
public String
getEncoding()
如果输入编码已知,则返回输入编码;如果未知,则返回 null。
return | 此实例的编码或 null |
getEncoding
String getEncoding()
- Return input encoding if known or null if unknown.
- Returns:
- the encoding of this instance or null
public boolean
hasText()
如果当前事件具有文本,则返回 true,否则返回 false。以下事件具有文本:CHARACTERS、DTD、ENTITY_REFERENCE、COMMENT、SPACE
英文文档:
hasText
boolean hasText()
- Return true if the current event has text, false otherwise
The following events have text:
CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT, SPACE
public Location
getLocation()
返回处理器的当前位置。如果 Location 未知,则处理器应返回 Location 的实现,该实现对位置返回 -1,对 publicId 和 systemId 返回 null。位置信息仅在调用 next() 之前有效。
英文文档:
getLocation
Location getLocation()
- Return the current location of the processor.
If the Location is unknown the processor should return
an implementation of Location that returns -1 for the
location and null for the publicId and systemId.
The location information is only valid until next() is
called.
public javax.xml.namespace.QName
getName()
返回当前 START_ELEMENT 或 END_ELEMENT 事件的 QName
return | 当前 START_ELEMENT 或 END_ELEMENT 事件的 QName |
Throws | IllegalStateException: 如果不是 START_ELEMENT 或 END_ELEMENT |
getName
QName getName()
- Returns a QName for the current START_ELEMENT or END_ELEMENT event
- Returns:
- the QName for the current START_ELEMENT or END_ELEMENT event
- Throws:
IllegalStateException
- if this is not a START_ELEMENT or END_ELEMENT
public String
getLocalName()
返回当前事件的(本地)名称。对于 START_ELEMENT 或 END_ELEMENT,返回当前元素的(本地)名称。对于 ENTITY_REFERENCE,返回实体名称。当前事件必须是 START_ELEMENT、END_ELEMENT 或 ENTITY_REFERENCE
return | localName |
Throws | IllegalStateException: 如果不是 START_ELEMENT、END_ELEMENT 或 ENTITY_REFERENCE |
getLocalName
String getLocalName()
- Returns the (local) name of the current event.
For START_ELEMENT or END_ELEMENT returns the (local) name of the current element.
For ENTITY_REFERENCE it returns entity name.
The current event must be START_ELEMENT or END_ELEMENT,
or ENTITY_REFERENCE
- Returns:
- the localName
- Throws:
IllegalStateException
- if this not a START_ELEMENT, END_ELEMENT or ENTITY_REFERENCE
public boolean
hasName()
如果当前事件有名称(是 START_ELEMENT 或 END_ELEMENT),则返回 ture,否则返回 false
英文文档:
hasName
boolean hasName()
- returns true if the current event has a name (is a START_ELEMENT or END_ELEMENT)
returns false otherwise
public String
getNamespaceURI()
如果当前事件是 START_ELEMENT 或 END_ELEMENT,则此方法返回前缀的 URI 或默认名称空间。如果事件没有前缀,则返回 null。
return | 绑定到此元素前缀的 URI、默认名称空间或 null |
getNamespaceURI
String getNamespaceURI()
- If the current event is a START_ELEMENT or END_ELEMENT this method
returns the URI of the prefix or the default namespace.
Returns null if the event does not have a prefix.
- Returns:
- the URI bound to this elements prefix, the default namespace, or null
public String
getPrefix()
返回当前事件的前缀,如果事件没有前缀,则返回 null
return | 前缀或 null |
getPrefix
String getPrefix()
- Returns the prefix of the current event or null if the event does not have a prefix
- Returns:
- the prefix or null
public String
getVersion()
获取在 xml 声明中声明的 xml 版本,如果没有声明版本,则返回 null
return | XML 版本或 null |
getVersion
String getVersion()
- Get the xml version declared on the xml declaration
Returns null if none was declared
- Returns:
- the XML version or null
public boolean
isStandalone()
获取 xml 声明中的独立声明
return | 如果是独立的,则返回 true,否则返回 false |
isStandalone
boolean isStandalone()
- Get the standalone declaration from the xml declaration
- Returns:
- true if this is standalone, or false otherwise
public boolean
standaloneSet()
检查文档中是否设置了独立
return | 如果文档中设置了独立,则返回 true,否则返回 false |
standaloneSet
boolean standaloneSet()
- Checks if standalone was set in the document
- Returns:
- true if standalone was set in the document, or false otherwise
public String
getCharacterEncodingScheme()
返回 xml 声明中声明的字符编码。如果没有声明字符编码,则返回 null
return | 在文档中声明的编码或 null |
getCharacterEncodingScheme
String getCharacterEncodingScheme()
- Returns the character encoding declared on the xml declaration
Returns null if none was declared
- Returns:
- the encoding declared in the document or null
public String
getPITarget()
获取处理指令的目标
return | 目标或 null |
getPITarget
String getPITarget()
- Get the target of a processing instruction
- Returns:
- the target or null
public String
getPIData()
获取处理指令的数据节
return | 数据或 null |
getPIData
String getPIData()
- Get the data section of a processing instruction
- Returns:
- the data or null
|
|||||||||
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 : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!