Top Description Fields Methods
org.jboss.modules.xml

public Interface XmlPullParser

Known Direct Implementers
org.jboss.modules.xml.MXParser
Imports
java.io.IOException, .InputStream, .Reader

XML Pull Parser is an interface that defines parsing functionlity provided in XMLPULL V1 API (visit this website to learn more about API and its implementations).

There are following different kinds of parser depending on which features are set:

There are two key methods: next() and nextToken(). While next() provides access to high level parsing events, nextToken() allows access to lower level tokens.

The current event state of the parser can be determined by calling the getEventType() method. Initially, the parser is in the START_DOCUMENT state.

The method next() advances the parser to the next event. The int value returned from next determines the current parser state and is identical to the value returned from following calls to getEventType ().

Th following event types are seen by next()

START_TAG
An XML start tag was read.
TEXT
Text content was read; the text content can be retreived using the getText() method. (when in validating mode next() will not report ignorable whitespaces, use nextToken() instead)
END_TAG
An end tag was read
END_DOCUMENT
No more events are available

after first next() or nextToken() (or any other next*() method) is called user application can obtain XML version, standalone and encoding from XML declaration in following ways:

A minimal example for using this API may look as follows:
import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException.html;
import org.xmlpull.v1.XmlPullParserFactory;

public class SimpleXmlPullApp
{

    public static void main (String args[])
        throws XmlPullParserException, IOException
    {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
         if(eventType == XmlPullParser.START_DOCUMENT) {
             System.out.println("Start document");
         } else if(eventType == XmlPullParser.END_DOCUMENT) {
             System.out.println("End document");
         } else if(eventType == XmlPullParser.START_TAG) {
             System.out.println("Start tag "+xpp.getName());
         } else if(eventType == XmlPullParser.END_TAG) {
             System.out.println("End tag "+xpp.getName());
         } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("Text "+xpp.getText());
         }
         eventType = xpp.next();
        }
    }
}

The above example will generate the following output:

Start document
Start tag foo
Text Hello World!
End tag foo

For more details on API usage, please refer to the quick Introduction available at http://www.xmlpull.org

Authors
Stefan Haustein, Aleksander Slominski
See Also
defineEntityReplacementText, getName, getNamespace, getText, next, nextToken, setInput, FEATURE_PROCESS_DOCDECL, FEATURE_VALIDATION, START_DOCUMENT, START_TAG, TEXT, END_TAG, END_DOCUMENT

Field Summary

Modifier and TypeField and Description
public static final int
CDSECT

A CDATA sections was just read; this token is available only from calls to nextToken().

public static final int
COMMENT

An XML comment was just read.

public static final int
DOCDECL

An XML document type declaration was just read.

public static final int
END_DOCUMENT

Logical end of the xml document.

public static final int
END_TAG

Returned from getEventType(), next(), or nextToken() when an end tag was read.

public static final int
ENTITY_REF

An entity reference was just read; this token is available from nextToken() only.

public static final String
FEATURE_PROCESS_DOCDECL

This feature determines whether the document declaration is processed.

public static final String
FEATURE_PROCESS_NAMESPACES

This feature determines whether the parser processes namespaces.

public static final String
FEATURE_REPORT_NAMESPACE_ATTRIBUTES

This feature determines whether namespace attributes are exposed via the attribute access methods.

public static final String
FEATURE_VALIDATION

If this feature is activated, all validation errors as defined in the XML 1.0 sepcification are reported.

public static final int
IGNORABLE_WHITESPACE

Ignorable whitespace was just read.

public static final String
NO_NAMESPACE

This constant represents the default namespace (empty string "")

public static final int
PROCESSING_INSTRUCTION

An XML processing instruction declaration was just read.

public static final int
START_DOCUMENT

Signalize that parser is at the very beginning of the document and nothing was read yet.

public static final int
START_TAG

Returned from getEventType(), next(), nextToken() when a start tag was read.

public static final int
TEXT

Character data was read and will is available by calling getText().

public static final String[]
TYPES

This array can be used to convert the event type integer constants such as START_TAG or TEXT to to a string.

Method Summary

Modifier and TypeMethod and Description
public void
defineEntityReplacementText(String entityName, String replacementText)

Set new value for entity replacement text as defined in XML 1.0 Section 4.5 Construction of Internal Entity Replacement Text.

public int
getAttributeCount()

Returns the number of attributes of the current start tag, or -1 if the current event type is not START_TAG

public String

Returns:

attribute name (null is never returned)
getAttributeName
(int
zero based index of attribute
index
)

Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces are disabled.

public String

Returns:

attribute namespace, empty string ("") is returned if namesapces processing is not enabled or namespaces processing is enabled but attribute has no namespace (it has no prefix).
getAttributeNamespace
(int
zero based index of attribute
index
)

Returns the namespace URI of the attribute with the given index (starts from 0).

public String

Returns:

attribute prefix or null if namespaces processing is not enabled.
getAttributePrefix
(int
zero based index of attribute
index
)

Returns the prefix of the specified attribute Returns null if the element has no prefix.

public String

Returns:

attribute type (null is never returned)
getAttributeType
(int
zero based index of attribute
index
)

Returns the type of the specified attribute If parser is non-validating it MUST return CDATA.

public String

Returns:

value of attribute (null is never returned)
getAttributeValue
(int
zero based index of attribute
index
)

Returns the given attributes value.

public String

Returns:

value of attribute or null if attribute with given name does not exist
getAttributeValue
(String
Namespace of the attribute if namespaces are enabled otherwise must be null
namespace
,
String
If namespaces enabled local name of attribute otherwise just attribute name
name
)

Returns the attributes value identified by namespace URI and namespace localName.

public int

Returns:

current column number or -1 if unknown.
getColumnNumber
()

Returns the current column number, starting from 0.

public int
getDepth()

Returns the current depth of the element.

public int
getEventType()

Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)

public boolean

Returns:

The value of the feature.
getFeature
(String
The name of feature to be retrieved.
name
)

Returns the current value of the given feature.

public String
getInputEncoding()

Returns the input encoding if known, null otherwise.

public int

Returns:

current line number or -1 if unknown.
getLineNumber
()

Returns the current line number, starting from 1.

public String
getName()

For START_TAG or END_TAG events, the (local) name of the current element is returned when namespaces are enabled.

public String
getNamespace(String prefix)

Returns the URI corresponding to the given prefix, depending on current state of the parser.

public String
getNamespace()

Returns the namespace URI of the current element.

public int
getNamespaceCount(int depth)

Returns the numbers of elements in the namespace stack for the given depth.

public String
getNamespacePrefix(int pos)

Returns the namespace prefixe for the given position in the namespace stack.

public String
getNamespaceUri(int pos)

Returns the namespace URI for the given position in the namespace stack If the position is out of range, an exception is thrown.

public String
getPositionDescription()

Returns a short text describing the current parser state, including the position, a description of the current event and the data source if known.

public String
getPrefix()

Returns the prefix of the current element.

public Object

Returns:

The value of named property.
getProperty
(String
The name of property to be retrieved.
name
)

Look up the value of a property.

public String
getText()

Returns the text content of the current event as String.

public char[]

Returns:

char buffer that contains the text of the current event (null if the current event has no text associated).
getTextCharacters
(int[]
Must hold an 2-element int array into which the start offset and length values will be written.
holderForStartAndLength
)

Returns the buffer that contains the text of the current event, as well as the start offset and length relevant for the current event.

public boolean

Returns:

false if attribute was in input
isAttributeDefault
(int
zero based index of attribute
index
)

Returns if the specified attribute was not in input was declared in XML.

public boolean
isEmptyElementTag()

Returns true if the current event is START_TAG and the tag is degenerated (e.g. <foobar/>).

public boolean
isWhitespace()

Checks whether the current TEXT event contains only whitespace characters.

public int
next()

Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole element content (comments and processing instructions will be ignored and emtity references must be expanded or exception mus be thrown if entity reerence can not be exapnded).

public int
nextTag()

Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception.

public String
nextText()

If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown.

public int
nextToken()

This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input.

public void
require(int type, String namespace, String name)

Test if the current event is of the given type and if the namespace and name do match.

public void
setFeature(String name, boolean state)

Use this call to change the general behaviour of the parser, such as namespace processing or doctype declaration handling.

public void
setInput(Reader in)

Set the input source for parser to the given reader and resets the parser.

public void
setInput(InputStream
contains a raw byte input stream of possibly unknown encoding (when inputEncoding is null).
inputStream
,
String
if not null it MUST be used as encoding for inputStream
inputEncoding
)

Sets the input stream the parser is going to process.

public void
setProperty(String name, Object value)

Set the value of a property.

Field Detail

CDSECTback to summary
public static final int CDSECT

A CDATA sections was just read; this token is available only from calls to nextToken(). A call to next() will accumulate various text events into a single event of type TEXT. The text contained in the CDATA section is available by callling getText().

See Also
nextToken, getText
COMMENTback to summary
public static final int COMMENT

An XML comment was just read. This event type is this token is available via nextToken() only; calls to next() will skip comments automatically. The content of the comment can be accessed using the getText() method.

See Also
nextToken, getText
DOCDECLback to summary
public static final int DOCDECL

An XML document type declaration was just read. This token is available from nextToken() only. The unparsed text inside the doctype is available via the getText() method.

See Also
nextToken, getText
END_DOCUMENTback to summary
public static final int END_DOCUMENT

Logical end of the xml document. Returned from getEventType, next() and nextToken() when the end of the input document has been reached.

Note

calling again next() or nextToken() will result in exception being thrown.

See Also
next, nextToken
END_TAGback to summary
public static final int END_TAG

Returned from getEventType(), next(), or nextToken() when an end tag was read. The name of start tag is available from getName(), its namespace and prefix are available from getNamespace() and getPrefix().

See Also
next, nextToken, getName, getPrefix, getNamespace, FEATURE_PROCESS_NAMESPACES
ENTITY_REFback to summary
public static final int ENTITY_REF

An entity reference was just read; this token is available from nextToken() only. The entity name is available by calling getName(). If available, the replacement text can be obtained by calling getTextt(); otherwise, the user is responsibile for resolving the entity reference. This event type is never returned from next(); next() will accumulate the replacement text and other text events to a single TEXT event.

See Also
nextToken, getText
FEATURE_PROCESS_DOCDECLback to summary
public static final String FEATURE_PROCESS_DOCDECL

This feature determines whether the document declaration is processed. If set to false, the DOCDECL event type is reported by nextToken() and ignored by next(). If this featue is activated, then the document declaration must be processed by the parser.

Note

If the document type declaration was ignored, entity references may cause exceptions later in the parsing process. The default value of this feature is false. It cannot be changed during parsing.

See Also
getFeature, setFeature
FEATURE_PROCESS_NAMESPACESback to summary
public static final String FEATURE_PROCESS_NAMESPACES

This feature determines whether the parser processes namespaces. As for all features, the default value is false.

Note

The value can not be changed during parsing an must be set before parsing.

See Also
getFeature, setFeature
FEATURE_REPORT_NAMESPACE_ATTRIBUTESback to summary
public static final String FEATURE_REPORT_NAMESPACE_ATTRIBUTES

This feature determines whether namespace attributes are exposed via the attribute access methods. Like all features, the default value is false. This feature cannot be changed during parsing.

See Also
getFeature, setFeature
FEATURE_VALIDATIONback to summary
public static final String FEATURE_VALIDATION

If this feature is activated, all validation errors as defined in the XML 1.0 sepcification are reported. This implies that FEATURE_PROCESS_DOCDECL is true and both, the internal and external document type declaration will be processed.

Note

This feature can not be changed during parsing. The default value is false.

See Also
getFeature, setFeature
IGNORABLE_WHITESPACEback to summary
public static final int IGNORABLE_WHITESPACE

Ignorable whitespace was just read. This token is available only from nextToken()). For non-validating parsers, this event is only reported by nextToken() when outside the root element. Validating parsers may be able to detect ignorable whitespace at other locations. The ignorable whitespace string is available by calling getText()

Note

this is different from calling the isWhitespace() method, since text content may be whitespace but not ignorable. Ignorable whitespace is skipped by next() automatically; this event type is never returned from next().

See Also
nextToken, getText
NO_NAMESPACEback to summary
public static final String NO_NAMESPACE

This constant represents the default namespace (empty string "")

PROCESSING_INSTRUCTIONback to summary
public static final int PROCESSING_INSTRUCTION

An XML processing instruction declaration was just read. This event type is available only via nextToken(). getText() will return text that is inside the processing instruction. Calls to next() will skip processing instructions automatically.

See Also
nextToken, getText
START_DOCUMENTback to summary
public static final int START_DOCUMENT

Signalize that parser is at the very beginning of the document and nothing was read yet. This event type can only be observed by calling getEvent() before the first call to next(), nextToken, or nextTag()).

See Also
next, nextToken
START_TAGback to summary
public static final int START_TAG

Returned from getEventType(), next(), nextToken() when a start tag was read. The name of start tag is available from getName(), its namespace and prefix are available from getNamespace() and getPrefix() if namespaces are enabled. See getAttribute* methods to retrieve element attributes. See getNamespace* methods to retrieve newly declared namespaces.

See Also
next, nextToken, getName, getPrefix, getNamespace, getAttributeCount, getDepth, getNamespaceCount, FEATURE_PROCESS_NAMESPACES
TEXTback to summary
public static final int TEXT

Character data was read and will is available by calling getText().

Note

next() will accumulate multiple events into one TEXT event, skipping IGNORABLE_WHITESPACE, PROCESSING_INSTRUCTION and COMMENT events, In contrast, nextToken() will stop reading text when any other event is observed. Also, when the state was reached by calling next(), the text value will be normalized, whereas getText() will return unnormalized content in the case of nextToken(). This allows an exact roundtrip without chnanging line ends when examining low level events, whereas for high level applications the text is normalized apropriately.

See Also
next, nextToken, getText
TYPESback to summary
public static final String[] TYPES

This array can be used to convert the event type integer constants such as START_TAG or TEXT to to a string. For example, the value of TYPES[START_TAG] is the string "START_TAG". This array is intended for diagnostic output only. Relying on the contents of the array may be dangerous since malicous applications may alter the array, although it is final, due to limitations of the Java language.

Method Detail

defineEntityReplacementTextback to summary
public void defineEntityReplacementText(String entityName, String replacementText) throws XmlPullParserException

Set new value for entity replacement text as defined in XML 1.0 Section 4.5 Construction of Internal Entity Replacement Text. If FEATURE_PROCESS_DOCDECL or FEATURE_VALIDATION are set, calling this function will result in an exception -- when processing of DOCDECL is enabled, there is no need to the entity replacement text manually.

The motivation for this function is to allow very small implementations of XMLPULL that will work in J2ME environments. Though these implementations may not be able to process the document type declaration, they still can work with known DTDs by using this function.

Please notes: The given value is used literally as replacement text and it corresponds to declaring entity in DTD that has all special characters escaped: left angle bracket is replaced with &lt;, ampersnad with &amp; and so on.

Note

The given value is the literal replacement text and must not contain any other entity reference (if it contains any entity reference there will be no further replacement).

Note

The list of pre-defined entity names will always contain standard XML entities such as amp (&amp;), lt (&lt;), gt (&gt;), quot (&quot;), and apos (&apos;). Those cannot be redefined by this method!

See Also
setInput, FEATURE_PROCESS_DOCDECL, FEATURE_VALIDATION
getAttributeCountback to summary
public int getAttributeCount()

Returns the number of attributes of the current start tag, or -1 if the current event type is not START_TAG

See Also
getAttributeNamespace, getAttributeName, getAttributePrefix, getAttributeValue
getAttributeNameback to summary
public String getAttributeName(int index)

Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces are disabled. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.

Parameters
index:int

zero based index of attribute

Returns:String

attribute name (null is never returned)

getAttributeNamespaceback to summary
public String getAttributeNamespace(int index)

Returns the namespace URI of the attribute with the given index (starts from 0). Returns an empty string ("") if namespaces are not enabled or the attribute has no namespace. Throws an IndexOutOfBoundsException if the index is out of range or the current event type is not START_TAG.

Note

if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set then namespace attributes (xmlns:ns='...') must be reported with namespace http://www.w3.org/2000/xmlns/ (visit this URL for description!). The default namespace attribute (xmlns="...") will be reported with empty namespace.

Note

The xml prefix is bound as defined in Namespaces in XML specification to "http://www.w3.org/XML/1998/namespace".

Parameters
index:int

zero based index of attribute

Returns:String

attribute namespace, empty string ("") is returned if namesapces processing is not enabled or namespaces processing is enabled but attribute has no namespace (it has no prefix).

getAttributePrefixback to summary
public String getAttributePrefix(int index)

Returns the prefix of the specified attribute Returns null if the element has no prefix. If namespaces are disabled it will always return null. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.

Parameters
index:int

zero based index of attribute

Returns:String

attribute prefix or null if namespaces processing is not enabled.

getAttributeTypeback to summary
public String getAttributeType(int index)

Returns the type of the specified attribute If parser is non-validating it MUST return CDATA.

Parameters
index:int

zero based index of attribute

Returns:String

attribute type (null is never returned)

getAttributeValueback to summary
public String getAttributeValue(int index)

Returns the given attributes value. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.

Note

attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL is false) as described in XML 1.0 section 3.3.3 Attribute-Value Normalization

Parameters
index:int

zero based index of attribute

Returns:String

value of attribute (null is never returned)

See Also
defineEntityReplacementText
getAttributeValueback to summary
public String getAttributeValue(String namespace, String name)

Returns the attributes value identified by namespace URI and namespace localName. If namespaces are disabled namespace must be null. If current event type is not START_TAG then IndexOutOfBoundsException will be thrown.

Note

attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL is false) as described in XML 1.0 section 3.3.3 Attribute-Value Normalization

Parameters
namespace:String

Namespace of the attribute if namespaces are enabled otherwise must be null

name:String

If namespaces enabled local name of attribute otherwise just attribute name

Returns:String

value of attribute or null if attribute with given name does not exist

See Also
defineEntityReplacementText
getColumnNumberback to summary
public int getColumnNumber()

Returns the current column number, starting from 0. When the parser does not know the current column number or can not determine it, -1 is returned (e.g. for WBXML).

Returns:int

current column number or -1 if unknown.

getDepthback to summary
public int getDepth()

Returns the current depth of the element. Outside the root element, the depth is 0. The depth is incremented by 1 when a start tag is reached. The depth is decremented AFTER the end tag event was observed.

<!-- outside -->     0
<root>               1
  sometext                 1
    <foobar>         2
    </foobar>        2
</root>              1
<!-- outside -->     0
getEventTypeback to summary
public int getEventType()

Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)

See Also
next(), nextToken()
getFeatureback to summary
public boolean getFeature(String name)

Returns the current value of the given feature.

Note

unknown features are always returned as false.

Parameters
name:String

The name of feature to be retrieved.

Returns:boolean

The value of the feature.

Exceptions
IllegalArgumentException:
if string the feature name is null
getInputEncodingback to summary
public String getInputEncoding()

Returns the input encoding if known, null otherwise. If setInput(InputStream, inputEncoding) was called with an inputEncoding value other than null, this value must be returned from this method. Otherwise, if inputEncoding is null and the parser suppports the encoding detection feature (http://xmlpull.org/v1/doc/features.html#detect-encoding), it must return the detected encoding. If setInput(Reader) was called, null is returned. After first call to next if XML declaration was present this method will return encoding declared.

getLineNumberback to summary
public int getLineNumber()

Returns the current line number, starting from 1. When the parser does not know the current line number or can not determine it, -1 is returned (e.g. for WBXML).

Returns:int

current line number or -1 if unknown.

getNameback to summary
public String getName()

For START_TAG or END_TAG events, the (local) name of the current element is returned when namespaces are enabled. When namespace processing is disabled, the raw name is returned. For ENTITY_REF events, the entity name is returned. If the current event is not START_TAG, END_TAG, or ENTITY_REF, null is returned.

Note

To reconstruct the raw element name when namespaces are enabled and the prefix is not null, you will need to add the prefix and a colon to localName..

getNamespaceback to summary
public String getNamespace(String prefix)

Returns the URI corresponding to the given prefix, depending on current state of the parser.

If the prefix was not declared in the current scope, null is returned. The default namespace is included in the namespace table and is available via getNamespace (null).

This method is a convenience method for

 for (int i = getNamespaceCount(getDepth ())-1; i >= 0; i--) {
  if (getNamespacePrefix(i).equals( prefix )) {
    return getNamespaceUri(i);
  }
 }
 return null;

Note

parser implementations may provide more efifcient lookup, e.g. using a Hashtable. The 'xml' prefix is bound to "http://www.w3.org/XML/1998/namespace", as defined in the Namespaces in XML specification. Analogous, the 'xmlns' prefix is resolved to http://www.w3.org/2000/xmlns/

See Also
getNamespaceCount, getNamespacePrefix, getNamespaceUri
getNamespaceback to summary
public String getNamespace()

Returns the namespace URI of the current element. The default namespace is represented as empty string. If namespaces are not enabled, an empty String ("") is always returned. The current event must be START_TAG or END_TAG; otherwise, null is returned.

getNamespaceCountback to summary
public int getNamespaceCount(int depth) throws XmlPullParserException

Returns the numbers of elements in the namespace stack for the given depth. If namespaces are not enabled, 0 is returned.

Note

when parser is on END_TAG then it is allowed to call this function with getDepth()+1 argument to retrieve position of namespace prefixes and URIs that were declared on corresponding START_TAG.

Note

to retrieve lsit of namespaces declared in current element:

      XmlPullParser pp = ...
      int nsStart = pp.getNamespaceCount(pp.getDepth()-1);
      int nsEnd = pp.getNamespaceCount(pp.getDepth());
      for (int i = nsStart; i < nsEnd; i++) {
         String prefix = pp.getNamespacePrefix(i);
         String ns = pp.getNamespaceUri(i);
          // ...
     }
See Also
getNamespacePrefix, getNamespaceUri, getNamespace(), getNamespace(String)
getNamespacePrefixback to summary
public String getNamespacePrefix(int pos) throws XmlPullParserException

Returns the namespace prefixe for the given position in the namespace stack. Default namespace declaration (xmlns='...') will have null as prefix. If the given index is out of range, an exception is thrown.

Note

when the parser is on an END_TAG, namespace prefixes that were declared in the corresponding START_TAG are still accessible although they are no longer in scope.

getNamespaceUriback to summary
public String getNamespaceUri(int pos) throws XmlPullParserException

Returns the namespace URI for the given position in the namespace stack If the position is out of range, an exception is thrown.

Note

when parser is on END_TAG then namespace prefixes that were declared in corresponding START_TAG are still accessible even though they are not in scope

getPositionDescriptionback to summary
public String getPositionDescription()

Returns a short text describing the current parser state, including the position, a description of the current event and the data source if known. This method is especially useful to provide meaningful error messages and for debugging purposes.

getPrefixback to summary
public String getPrefix()

Returns the prefix of the current element. If the element is in the default namespace (has no prefix), null is returned. If namespaces are not enabled, or the current event is not START_TAG or END_TAG, null is returned.

getPropertyback to summary
public Object getProperty(String name)

Look up the value of a property. The property name is any fully-qualified URI.

Note

unknown properties are always returned as null.

Parameters
name:String

The name of property to be retrieved.

Returns:Object

The value of named property.

getTextback to summary
public String getText()

Returns the text content of the current event as String. The value returned depends on current event type, for example for TEXT event it is element content (this is typical case when next() is used). See description of nextToken() for detailed description of possible returned values for different types of events.

Note

in case of ENTITY_REF, this method returns the entity replacement text (or null if not available). This is the only case where getText() and getTextCharacters() return different values.

See Also
getEventType, next, nextToken
getTextCharactersback to summary
public char[] getTextCharacters(int[] holderForStartAndLength)

Returns the buffer that contains the text of the current event, as well as the start offset and length relevant for the current event. See getText(), next() and nextToken() for description of possible returned values.

Note

this buffer must not be modified and its content MAY change after a call to next() or nextToken(). This method will always return the same value as getText(), except for ENTITY_REF. In the case of ENTITY ref, getText() returns the replacement text and this method returns the actual input buffer containing the entity name. If getText() returns null, this method returns null as well and the values returned in the holder array MUST be -1 (both start and length).

Parameters
holderForStartAndLength:int[]

Must hold an 2-element int array into which the start offset and length values will be written.

Returns:char[]

char buffer that contains the text of the current event (null if the current event has no text associated).

See Also
getText, next, nextToken
isAttributeDefaultback to summary
public boolean isAttributeDefault(int index)

Returns if the specified attribute was not in input was declared in XML. If parser is non-validating it MUST always return false. This information is part of XML infoset:

Parameters
index:int

zero based index of attribute

Returns:boolean

false if attribute was in input

isEmptyElementTagback to summary
public boolean isEmptyElementTag() throws XmlPullParserException

Returns true if the current event is START_TAG and the tag is degenerated (e.g. <foobar/>).

Note

if the parser is not on START_TAG, an exception will be thrown.

isWhitespaceback to summary
public boolean isWhitespace() throws XmlPullParserException

Checks whether the current TEXT event contains only whitespace characters. For IGNORABLE_WHITESPACE, this is always true. For TEXT and CDSECT, false is returned when the current event text contains at least one non-white space character. For any other event type an exception is thrown.

Note

non-validating parsers are not able to distinguish whitespace and ignorable whitespace, except from whitespace outside the root element. Ignorable whitespace is reported as separate event, which is exposed via nextToken only.

nextback to summary
public int next() throws XmlPullParserException, IOException

Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole element content (comments and processing instructions will be ignored and emtity references must be expanded or exception mus be thrown if entity reerence can not be exapnded). If element content is empty (content is "") then no TEXT event will be reported.

Note

empty element (such as <tag/>) will be reported with two separate events: START_TAG, END_TAG - it must be so to preserve parsing equivalency of empty element to <tag></tag>. (see isEmptyElementTag ())

See Also
isEmptyElementTag, START_TAG, TEXT, END_TAG, END_DOCUMENT
nextTagback to summary
public int nextTag() throws XmlPullParserException, IOException

Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception. It will skip whitespace TEXT before actual tag if any.

essentially it does this

  int eventType = next();
  if(eventType == TEXT &&  isWhitespace()) {   // skip whitespace
     eventType = next();
  }
  if (eventType != START_TAG &&  eventType != END_TAG) {
     throw new XmlPullParserException("expected start or end tag", this, null);
  }
  return eventType;
nextTextback to summary
public String nextText() throws XmlPullParserException, IOException

If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown. After calling this function successfully parser will be positioned on END_TAG.

The motivation for this function is to allow to parse consistently both empty elements and elements that has non empty content, for example for input:

  1. <tag>foo</tag>
  2. <tag></tag> (which is equivalent to <tag/> both input can be parsed with the same code:
      p.nextTag()
      p.requireEvent(p.START_TAG, "", "tag");
      String content = p.nextText();
      p.requireEvent(p.END_TAG, "", "tag");
    
    This function together with nextTag make it very easy to parse XML that has no mixed content.

    Essentially it does this

     if(getEventType() != START_TAG) {
        throw new XmlPullParserException(
          "parser must be on START_TAG to read next text", this, null);
     }
     int eventType = next();
     if(eventType == TEXT) {
        String result = getText();
        eventType = next();
        if(eventType != END_TAG) {
          throw new XmlPullParserException(
             "event TEXT it must be immediately followed by END_TAG", this, null);
         }
         return result;
     } else if(eventType == END_TAG) {
        return "";
     } else {
        throw new XmlPullParserException(
          "parser must be on START_TAG or TEXT to read text", this, null);
     }
    
nextTokenback to summary
public int nextToken() throws XmlPullParserException, IOException

This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input.

If special feature FEATURE_XML_ROUNDTRIP (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip) is enabled it is possible to do XML document round trip ie. reproduce exectly on output the XML input using getText(): returned content is always unnormalized (exactly as in input). Otherwise returned content is end-of-line normalized as described XML 1.0 End-of-Line Handling and. Also when this feature is enabled exact content of START_TAG, END_TAG, DOCDECL and PROCESSING_INSTRUCTION is available.

Here is the list of tokens that can be returned from nextToken() and what getText() and getTextCharacters() returns:

START_DOCUMENT
null
END_DOCUMENT
null
START_TAG
null unless FEATURE_XML_ROUNDTRIP enabled and then returns XML tag, ex: <tag attr='val'>
END_TAG
null unless FEATURE_XML_ROUNDTRIP id enabled and then returns XML tag, ex: </tag>
TEXT
return element content.

Note

that element content may be delivered in multiple consecutive TEXT events.

IGNORABLE_WHITESPACE
return characters that are determined to be ignorable white space. If the FEATURE_XML_ROUNDTRIP is enabled all whitespace content outside root element will always reported as IGNORABLE_WHITESPACE otherise rteporting is optional.

Note

that element content may be delevered in multiple consecutive IGNORABLE_WHITESPACE events.

CDSECT
return text inside CDATA (ex. 'fo<o' from <!CDATA[fo<o]]>)
PROCESSING_INSTRUCTION
if FEATURE_XML_ROUNDTRIP is true return exact PI content ex: 'pi foo' from <?pi foo?> otherwise it may be exact PI content or concatenation of PI target, space and data so for example for <?target data?> string "target data" may be returned if FEATURE_XML_ROUNDTRIP is false.
COMMENT
return comment content ex. 'foo bar' from <!--foo bar-->
ENTITY_REF
getText() MUST return entity replacement text if PROCESS_DOCDECL is false otherwise getText() MAY return null, additionally getTextCharacters() MUST return entity name (for example 'entity_name' for &entity_name;)

Note

this is the only place where value returned from getText() and getTextCharacters() are different
NOTE: it is user responsibility to resolve entity reference if PROCESS_DOCDECL is false and there is no entity replacement text set in defineEntityReplacementText() method (getText() will be null)
NOTE: character entities (ex. &#32;) and standard entities such as &amp; &lt; &gt; &quot; &apos; are reported as well and are not reported as TEXT tokens but as ENTITY_REF tokens! This requirement is added to allow to do roundtrip of XML documents!

DOCDECL
if FEATURE_XML_ROUNDTRIP is true or PROCESS_DOCDECL is false then return what is inside of DOCDECL for example it returns:
" titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
[<!ENTITY % active.links "INCLUDE">]"

for input document that contained:

<!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
[<!ENTITY % active.links "INCLUDE">]>
otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true then what is returned is undefined (it may be even null)

Note

there is no gurantee that there will only one TEXT or IGNORABLE_WHITESPACE event from nextToken() as parser may chose to deliver element content in multiple tokens (dividing element content into chunks)

Note

whether returned text of token is end-of-line normalized is depending on FEATURE_XML_ROUNDTRIP.

Note

XMLDecl (<?xml ...?>) is not reported but its content is available through optional properties (see class description above).

See Also
next, START_TAG, TEXT, END_TAG, END_DOCUMENT, COMMENT, DOCDECL, PROCESSING_INSTRUCTION, ENTITY_REF, IGNORABLE_WHITESPACE
requireback to summary
public void require(int type, String namespace, String name) throws XmlPullParserException, IOException

Test if the current event is of the given type and if the namespace and name do match. null will match any namespace and any name. If the test is not passed, an exception is thrown. The exception text indicates the parser position, the expected event and the current event that is not meeting the requirement.

Essentially it does this

 if (type != getEventType()
 || (namespace != null &&  !namespace.equals( getNamespace () ) )
 || (name != null &&  !name.equals( getName() ) ) )
    throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription());
setFeatureback to summary
public void setFeature(String name, boolean state) throws XmlPullParserException

Use this call to change the general behaviour of the parser, such as namespace processing or doctype declaration handling. This method must be called before the first call to next or nextToken. Otherwise, an exception is thrown.

Example: call setFeature(FEATURE_PROCESS_NAMESPACES, true) in order to switch on namespace processing. The initial settings correspond to the properties requested from the XML Pull Parser factory. If none were requested, all feautures are deactivated by default.

Exceptions
XmlPullParserException:
If the feature is not supported or can not be set
IllegalArgumentException:
If string with the feature name is null
setInputback to summary
public void setInput(Reader in) throws XmlPullParserException

Set the input source for parser to the given reader and resets the parser. The event type is set to the initial value START_DOCUMENT. Setting the reader to null will just stop parsing and reset parser state, allowing the parser to free internal resources such as parsing buffers.

setInputback to summary
public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException

Sets the input stream the parser is going to process. This call resets the parser state and sets the event type to the initial value START_DOCUMENT.

Note

If an input encoding string is passed, it MUST be used. Otherwise, if inputEncoding is null, the parser SHOULD try to determine input encoding following XML 1.0 specification (see below). If encoding detection is supported then following feature http://xmlpull.org/v1/doc/features.html#detect-encoding MUST be true amd otherwise it must be false

Parameters
inputStream:InputStream

contains a raw byte input stream of possibly unknown encoding (when inputEncoding is null).

inputEncoding:String

if not null it MUST be used as encoding for inputStream

setPropertyback to summary
public void setProperty(String name, Object value) throws XmlPullParserException

Set the value of a property. The property name is any fully-qualified URI.

Exceptions
XmlPullParserException:
If the property is not supported or can not be set
IllegalArgumentException:
If string with the property name is null