|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 only two key methods: next() and nextToken() that provides access to high level parsing events and to lower level tokens.
The parser is always in some state and current state can be determined by calling getType() mehod. Initially parser is in START_DOCUMENT state.
Method next() return int that contains identifier of parsing event. This method can return following events (and will change parser state to the returned event):
import java.io.IOException; import java.io.StringReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; 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 ( "Hello World! " ) ); int eventType = xpp.getEventType(); while (eventType != xpp.END_DOCUMENT) { if(eventType == xpp.START_DOCUMENT) { System.out.println("Start document"); } else if(eventType == xpp.END_DOCUMENT) { System.out.println("End document"); } else if(eventType == xpp.START_TAG) { System.out.println("Start tag "+xpp.getName()); } else if(eventType == xpp.END_TAG) { System.out.println("End tag "+xpp.getName()); } else if(eventType == xpp.TEXT) { System.out.println("Text "+xpp.getText()); } eventType = xpp.next(); } } }
When run it will produce following output:
Start document Start tag foo Text Hello World! End tag foo
For more details on use of API please read Quick Introduction available at http://www.xmlpull.org
XmlPullParserFactory
Field Summary | |
static byte |
CDSECT
TOKEN: CDATA sections was just read (this token is available only from nextToken()). |
static int |
COMMENT
TOKEN: XML comment was just read and getText() will return value inside comment (this token is available only from nextToken()). |
static int |
DOCDECL
TOKEN: XML DOCTYPE declaration was just read and getText() will return text that is inside DOCDECL (this token is available only from nextToken()). |
static int |
END_DOCUMENT
EVENT TYPE and TOKEN: logical end of xml document (available from next() and nextToken()). |
static int |
END_TAG
EVENT TYPE and TOKEN: end tag was just read (available from next() and nextToken()). |
static byte |
ENTITY_REF
TOKEN: Entity reference was just read (this token is available only from nextToken()). |
static java.lang.String |
FEATURE_PROCESS_DOCDECL
FEATURE: Processing of DOCDECL is by default set to false and if DOCDECL is encountred it is reported by nextToken() and ignored by next(). |
static java.lang.String |
FEATURE_PROCESS_NAMESPACES
FEATURE: Processing of namespaces is by default set to false. |
static java.lang.String |
FEATURE_REPORT_NAMESPACE_ATTRIBUTES
FEATURE: Report namespace attributes also - they can be distinguished looking for prefix == "xmlns" or prefix == null and name == "xmlns it is off by default and only meningful when FEATURE_PROCESS_NAMESPACES feature is on. |
static java.lang.String |
FEATURE_VALIDATION
FEATURE: Report all validation errors as defined by XML 1.0 sepcification (implies that PROCESS_DOCDECL is true and both internal and external DOCDECL will be processed). |
static byte |
IGNORABLE_WHITESPACE
TOKEN: Ignorable whitespace was just read (this token is available only from nextToken()). |
static java.lang.String |
NO_NAMESPACE
This constant represents lack of or default namespace (empty string "") |
static byte |
PROCESSING_INSTRUCTION
TOKEN: XML processing instruction declaration was just read and getText() will return text that is inside processing instruction (this token is available only from nextToken()). |
static int |
START_DOCUMENT
EVENT TYPE and TOKEN: signalize that parser is at the very beginning of the document and nothing was read yet - the parser is before first call to next() or nextToken() (available from next() and nextToken()). |
static int |
START_TAG
EVENT TYPE and TOKEN: start tag was just read (available from next() and nextToken()). |
static int |
TEXT
EVENT TYPE and TOKEN: character data was read and will be available by call to getText(). |
static java.lang.String[] |
TYPES
Use this array to convert evebt type number (such as START_TAG) to to string giving event name, ex: "START_TAG" == TYPES[START_TAG] |
Method Summary | |
void |
defineCharacterEntity(java.lang.String entity,
java.lang.String value)
Set new value for entity. |
int |
getAttributeCount()
Returns the number of attributes on the current element; -1 if the current event is not START_TAG |
java.lang.String |
getAttributeName(int index)
Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces are disabled. |
java.lang.String |
getAttributeNamespace(int index)
Returns the namespace URI of the specified attribute number index (starts from 0). |
java.lang.String |
getAttributePrefix(int index)
Returns the prefix of the specified attribute Returns null if the element has no prefix. |
java.lang.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. |
java.lang.String |
getAttributeValue(java.lang.String namespace,
java.lang.String name)
Returns the attributes value identified by namespace URI and namespace localName. |
int |
getColumnNumber()
Current columnt |
int |
getDepth()
Returns the current depth of the element. |
int |
getEventType()
Returns the type of the current event (START_TAG, END_TAG, CONTENT, etc.) |
boolean |
getFeature(java.lang.String name)
Return the current value of the feature with given name. |
int |
getLineNumber()
Current line |
java.lang.String |
getName()
Returns the (local) name of the current element when namespaces are enabled or raw name when namespaces are disabled. |
java.lang.String |
getNamespace()
Returns the namespace URI of the current element. |
java.lang.String |
getNamespace(java.lang.String prefix)
Return uri for the given prefix. |
int |
getNamespaceCount(int depth)
Return position in stack of first namespace slot for element at passed depth. |
java.lang.String |
getNamespacePrefix(int pos)
Return namespace prefixes for position pos in namespace stack |
java.lang.String |
getNamespaceUri(int pos)
Return namespace URIs for position pos in namespace stack If pos id out of range it throw exception. |
java.lang.String |
getPositionDescription()
Short text describing parser position, including a description of the current event and data source if known and if possible what parser was seeing lastly in input. |
java.lang.String |
getPrefix()
Returns the prefix of the current element or null if elemet has no prefix (is in defualt namespace). |
java.lang.Object |
getProperty(java.lang.String name)
Look up the value of a property. |
java.lang.String |
getText()
Read text content of the current event as String. |
char[] |
getTextCharacters(int[] holderForStartAndLength)
Get the buffer that contains text of the current event and start offset of text is passed in first slot of input int array and its length is in second slot. |
boolean |
isEmptyElementTag()
Returns true if the current event is START_TAG and the tag is degenerated (e.g. |
boolean |
isWhitespace()
Check if current TEXT event contains only whitespace characters. |
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 throw if entity reerence can not be exapnded). |
int |
nextToken()
This method works similarly to next() but will expose additional event types (COMMENT, DOCDECL, PROCESSING_INSTRUCTION, ENTITY_REF or IGNORABLE_WHITESPACE) if they are available in input. |
java.lang.String |
readText()
If the current event is text, the value of getText is returned and next() is called. |
void |
require(int type,
java.lang.String namespace,
java.lang.String name)
test if the current event is of the given type and if the namespace and name do match. |
void |
setFeature(java.lang.String name,
boolean state)
Use this call to change the general behaviour of the parser, such as namespace processing or doctype declaration handling. |
void |
setInput(java.io.Reader in)
Set the input for parser. |
void |
setProperty(java.lang.String name,
java.lang.Object value)
Set the value of a property. |
Field Detail |
public static final java.lang.String NO_NAMESPACE
public static final int START_DOCUMENT
next()
,
nextToken()
public static final int END_DOCUMENT
NOTE: calling again next() or nextToken() will result in exception being thrown.
next()
,
nextToken()
public static final int START_TAG
next()
,
nextToken()
,
getName()
,
getPrefix()
,
getNamespace(java.lang.String)
,
getAttributeCount()
,
getDepth()
,
getNamespaceCount(int)
,
getNamespace(java.lang.String)
,
FEATURE_PROCESS_NAMESPACES
public static final int END_TAG
next()
,
nextToken()
,
getName()
,
getPrefix()
,
getNamespace(java.lang.String)
,
FEATURE_PROCESS_NAMESPACES
public static final int TEXT
NOTE: this event is returned returned both by nextToken() and next(), all following events are only returned by nextToken().
NOTE: next() will (in contrast to nextToken ()) accumulate multiple TEXT events into one, skipping inbetween IGNORABLE_WHITESPACE, PROCESSING_INSTRUCTION and COMMENT events.
next()
,
nextToken()
,
getText()
public static final byte CDSECT
nextToken()
,
getText()
public static final byte ENTITY_REF
nextToken()
,
getName()
,
getText()
public static final byte IGNORABLE_WHITESPACE
NOTE: this is different than call isWhitespace() as element content may be whitespace but may not be ignorable whitespace.
nextToken()
,
getText()
public static final byte PROCESSING_INSTRUCTION
nextToken()
,
getText()
public static final int COMMENT
nextToken()
,
getText()
public static final int DOCDECL
nextToken()
,
getText()
public static final java.lang.String[] TYPES
public static final java.lang.String FEATURE_PROCESS_NAMESPACES
NOTE: can not be changed during parsing!
getFeature(java.lang.String)
,
setFeature(java.lang.String, boolean)
public static final java.lang.String FEATURE_REPORT_NAMESPACE_ATTRIBUTES
NOTE: can not be changed during parsing!
getFeature(java.lang.String)
,
setFeature(java.lang.String, boolean)
public static final java.lang.String FEATURE_PROCESS_DOCDECL
NOTE: if the DOCDECL was ignored further in parsing there may be fatal exception when undeclared entity is encountered!
NOTE: can not be changed during parsing!
getFeature(java.lang.String)
,
setFeature(java.lang.String, boolean)
public static final java.lang.String FEATURE_VALIDATION
NOTE: can not be changed during parsing!
getFeature(java.lang.String)
,
setFeature(java.lang.String, boolean)
Method Detail |
public void setFeature(java.lang.String name, boolean state) throws XmlPullParserException
Example: Use setFeature (FEATURE_PROCESS_NAMESPACES, true) in order to switch on namespace processing. Default settings correspond to properties requested from the XML Pull Parser factory (if none were requested then all feautures are by default false).
XmlPullParserException
- if feature is not supported or can not be setjava.lang.IllegalArgumentException
- if feature string is nullpublic boolean getFeature(java.lang.String name)
NOTE: unknown features are
name
- The name of feature to be retrieved.java.lang.IllegalArgumentException
- if feature string is null
public void setProperty(java.lang.String name, java.lang.Object value) throws XmlPullParserException
public java.lang.Object getProperty(java.lang.String name)
NOTE: unknown features are
name
- The name of property to be retrieved.
public void setInput(java.io.Reader in) throws XmlPullParserException
public void defineCharacterEntity(java.lang.String entity, java.lang.String value) throws XmlPullParserException
NOTE: list of entites will be reset to standard XML entities (such as & < >) after each call to setInput
setInput(java.io.Reader)
public int getNamespaceCount(int depth) throws XmlPullParserException
NOTE: default namespace is not included in namespace table but available by getNamespace() and not available form getNamespace(String)
getNamespacePrefix(int)
,
getNamespaceUri(int)
,
getNamespace()
,
getNamespace(String)
public java.lang.String getNamespacePrefix(int pos) throws XmlPullParserException
public java.lang.String getNamespaceUri(int pos) throws XmlPullParserException
public java.lang.String getNamespace(java.lang.String prefix) throws XmlPullParserException
It will return null if namespace could not be found. (should we throw an exception in that case?)
Convenience method forfor (int i = getNamespaceCount (getDepth ())-1; i >= 0; i--) { if (getNamespacePrefix (i).equals (prefix)) { return getNamespaceUri (i); } } return null;
However parser implementation can be more efficinet about.
getNamespaceCount(int)
,
getNamespacePrefix(int)
,
getNamespaceUri(int)
public int getDepth()
<!-- outside --> 0 <root> 1 sometext 1 <foobar> 2 </foobar> 2 </root> 1 <!-- outside --> 0 </pre>
public java.lang.String getPositionDescription()
public int getLineNumber()
public int getColumnNumber()
public boolean isWhitespace() throws XmlPullParserException
NOTE: it can be only called for element content related events such as TEXT, CDSECT, IGNORABLE_WHITESPACE and ENTITY_REF otherwise exception will be thrown.
public java.lang.String getText()
NOTE: parser must be on TEXT, COMMENT, PROCESSING_INSTRUCTION or DOCDECL event; otherwise, null is returned.
public char[] getTextCharacters(int[] holderForStartAndLength)
NOTE: this buffer must not be modified and its content MAY change after call to next() or nextToken().
NOTE: parser must be on TEXT, COMMENT, PROCESSING_INSTRUCTION or DOCDECL event; otherwise, null is returned and values returned in holder MUST be -1 (both start and length).
holderForStartAndLength
- the 2-element int array into which
values of start offset and length will be written into frist and second slot of array.getText()
public java.lang.String getNamespace()
public java.lang.String getName()
NOTE: to reconstruct raw element name when namespaces are enabled you will need to add prefix and colon to localName if prefix is not null.
public java.lang.String getPrefix()
public boolean isEmptyElementTag() throws XmlPullParserException
NOTE: if parser is not on START_TAG then the exception will be thrown.
public int getAttributeCount()
getAttributeNamespace(int)
,
getAttributeName(int)
,
getAttributePrefix(int)
,
getAttributeValue(int)
public java.lang.String getAttributeNamespace(int index)
zero
- based index of attributepublic java.lang.String getAttributeName(int index)
zero
- based index of attributepublic java.lang.String getAttributePrefix(int index)
zero
- based index of attributepublic java.lang.String getAttributeValue(int index)
zero
- based index of attributepublic java.lang.String getAttributeValue(java.lang.String namespace, java.lang.String name)
namespace
- Namespace of the attribute if namespaces are enabled otherwise must be nullname
- If namespaces enabled local name of attribute otherwise just attribute namepublic int getEventType() throws XmlPullParserException
next()
,
nextToken()
public int next() throws XmlPullParserException, java.io.IOException
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 ())
isEmptyElementTag()
,
START_TAG
,
TEXT
,
END_TAG
,
END_DOCUMENT
public int nextToken() throws XmlPullParserException, java.io.IOException
If special feature FEATURE_XML_ROUNDTRIP (http://xmlpull.org/v1/features/xml-roundtrip) is true then it is possible to do XML document round trip ie. reproduce exectly on output the XML input using getText().
Here is the list of tokens that can be returned from nextToken() and what getText() and getTextCharacters() returns:
NOTE: retirned text of token is not end-of-line normalized.
next()
,
START_TAG
,
TEXT
,
END_TAG
,
END_DOCUMENT
,
COMMENT
,
DOCDECL
,
PROCESSING_INSTRUCTION
,
ENTITY_REF
,
IGNORABLE_WHITESPACE
public void require(int type, java.lang.String namespace, java.lang.String name) throws XmlPullParserException, java.io.IOException
essentially it does this
if (getType() == TEXT && type != TEXT && isWhitespace ()) next (); if (type != getType || (namespace != null && !namespace.equals (getNamespace ())) || (name != null && !name.equals (getName ()) throw new XmlPullParserException ( "....");
public java.lang.String readText() throws XmlPullParserException, java.io.IOException
essentially it does this
if (getType != TEXT) return "" String result = getText (); next (); return result;
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |