|
|||||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Interface Summary | |
XmlEndTag | This represent XML end tag. |
XmlFormatter | This interface extends XmlRecorder to give some control over formattingof XML output. |
XmlNode | This class represents XML subtree. |
XmlPullNode | This class represents pullable XML subtree - children are built on demand. |
XmlPullParser | Generic interface for simple and quick XML Pull Parser. |
XmlPullParserBufferControl | Additional interface to control XML Pull Parser buffering. |
XmlPullParserEventPosition | Special interface to retrieve event positioning information. |
XmlRecorder | This class represents abstract functionality necessary to to persist XML Pull Parser events. |
XmlStartTag | This class represents abstract functionality necessary to to persist XML Pull Parser events. |
XmlTag | Base interface that encapsulates common functionality for XML elements: both start tag and end tag (an empty element is equivalent to start tag followed by end tag so for simplicity ti is not modeled as a separate class). |
XmlWritable | This interface can be used by classes that wishes to implement its own way to persist XML into writer. |
Class Summary | |
XmlPullParserFactory | This class is used to create implementations of XML Pull Parser. |
Exception Summary | |
XmlPullParserException | This exception is thrown to signal XML Pull Parser related excepions. |
This package defines classes compromising public API of XML Pull Parser 2.0 (org.gjt.xpp).
Note this package is deprecated by next version of XML Pull Parser called XPP3/MXP1 that implements XmlPull API.
Usage:
Xml Pull Parser (XPP) provides a simple and fast implementation of "pull parsing model" that allows processing application to request parsing events incrementally (ideal for deserializing XML such as SOAP encoding).
Following steps are required to use XPP:
XmlPullParserFactory
using
newInstance() method
XmlPullParser
using
newPullParser() method on instance of XmlPullParserFactory
XmlPullParser.setInput(Reader)
or
XmlPullParser.setInput(char[])
XmlPullParser.next()
- this method returns event type and
parsing is finished when it returns
XmlPullParser.END_DOCUMENT
event type.
Typically parsing is done in while loop that will
work until XmlPullParser.next()
returns
XmlPullParser.END_DOCUMENT
event.
All possible types of events
that XmlPullParser.next()
can return:
XmlPullParser.END_DOCUMENT
to signal that parsing is finished
XmlPullParser.START_TAG
- user can now call
XmlPullParser.readStartTag(org.gjt.xpp.XmlStartTag)
to get start tag information including
element name, (if namespaces are supported, uri and localName can also
be obtained) and attributes
XmlPullParser.END_TAG
- end tag was read and user can call
XmlPullParser.readEndTag(org.gjt.xpp.XmlEndTag)
to get end tag information
XmlPullParser.CONTENT
element constent was read and user can
call XmlPullParser.readContent()
to get it
If there is parsing error XmlPullParser.next()
will throw
XmlPullParserException
.
An example Java program may look like this:
(for more detailed example please see
src/java/samples/
)
// 1. creating instance of parser XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser pp = factory.newPullParser(); // 2. setting options // ex. disabling mixed content for elements // (element can not have elements mixed with non-whitespace string content) pp.setAllowedMixedContent(false); // 3. setting input String data = "<hello>World!</hello>"; // input will be taken from java.io.Reader pp.setInput(new StringReader(data)); // input could be also taken from String directly: //pp.setInput(data.toCharArray()); // 4. parsing //declare variables used during parsing byte type; // received event type XmlStartTag stag = factory.newStartTag(); XmlEndTag etag = factory.newEndTag(); // start parsing loop while((type = pp.next()) != XmlPullParser.END_DOCUMENT) { if(type == XmlPullParser.CONTENT) { String s = pp.readContent(); System.out.println("CONTENT={'"+s+"'}"); } else if(type == XmlPullParser.END_TAG) { pp.readEndTag(etag); System.out.println(""+etag); } else if(type == XmlPullParser.START_TAG) { pp.readStartTag(stag); System.out.println(""+stag); } }
After parsing is finished, parser instance may be reused
by calling again XmlPullParser.setInput(Reader)
.
Example Java code that will read string for SOAP encoding
public String readString(XmlPullParser pp, XmlStartTag stag) throws DeserializeException, XmlPullParserException, IOException { String xs = stag.getAttributeValue(Soap.XSI_NS, "null"); if( "1".equals(xs) ) { if(pp.next() != XmlPullParser.END_TAG) throw new DeserializeException("expected end tag"); return null; } if(pp.next() != XmlPullParser.CONTENT) throw new DeserializeException("expected content"); String content = pp.readContent(); if(pp.next() != XmlPullParser.END_TAG) throw new DeserializeException("expected end tag"); return content; }
|
|||||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |