Overview
A parser for structured data. The parser supplies a sequence of Events as it process the document under application control.
XML and JSON are the two data formats currently supported. The parser can be used with the onEvent callback like this -var total; var title; def parser = PullParser { documentType: PullParser.XML; input: anInputStreamThatContainsXML; onEvent: function(event: Event) { if (event.type == PullParser.START_ELEMENT) { if (event.qname.name == "ResultSet" and event.level == 0) { total = event.getAttributeValue(QName{name:"totalResultsAvailable"}); } } else if (event.type == PullParser.END_ELEMENT) { if (event.qname.name == "Title" and event.level == 2) { title = event.text; } } else if (event.type == PullParser.END_DOCUMENT) { input.close(); println("results: {total}, title: {title}"); } } } parser.parse();The parser can also be used in "linear" mode where the application simply pulls events, discarding or skipping over those it is not interested in. The current event is always available as event or via the onEvent callback. For example,
parser.onEvent = function(event: Event) { println(event) } parser.forward(); parser.forward(2); parser.seek(QName{name:"child"}); parser.seek(QName{name:"child" namespace:"urn:some.namespace.uri"}, 2);The seek and forward functions can be chained together, for example,
parser.seek("child", 3).forward(2);
See Also:
Event
Profile: common
Script Variable Summary
access | name | type | Can Read | Can Init | Can Write | Default Value | description |
---|---|---|---|---|---|---|---|
public | CDATA | Integer |
Value of Event.type indicating CDATA in an XML document |
||||
public | DEFAULT_ENCODING | String |
The default character encoding is utf-8 |
||||
public | END_ARRAY | Integer |
Value of Event.type indicating the end of a JSON array |
||||
public | END_ARRAY_ELEMENT | Integer |
Value of Event.type indicating the end of a JSON array element |
||||
public | END_DOCUMENT | Integer |
Value of Event.type indicating the end of an XML or JSON document |
||||
public | END_ELEMENT | Integer |
Value of Event.type indicating the end of an XML element or JSON object |
||||
public | END_VALUE | Integer |
Value of Event.type indicating the end of a JSON object value |
||||
public | FALSE | Integer |
Value of Event.type indicating a JSON false value |
||||
public | INTEGER | Integer |
Value of Event.type indicating a JSON integer |
||||
public | JSON | String |
Value of documentType for JSON |
||||
public | NULL | Integer |
Value of Event.type indicating a JSON null value |
||||
public | NUMBER | Integer |
Value of Event.type indicating a JSON floating-point number |
||||
public | START_ARRAY | Integer |
Value of Event.type indicating the start of a JSON array |
||||
public | START_ARRAY_ELEMENT | Integer |
Value of Event.type indicating the start of a JSON array element |
||||
public | START_DOCUMENT | Integer |
Value of Event.type indicating the start of an XML or JSON document |
||||
public | START_ELEMENT | Integer |
Value of Event.type indicating the start of an XML element or JSON object |
||||
public | START_VALUE | Integer |
Value of Event.type indicating the start of a JSON object value |
||||
public | TEXT | Integer |
Value of Event.type indicating text in an XML or JSON element |
||||
public | TRUE | Integer |
Value of Event.type indicating a JSON true value |
||||
public | UNKNOWN | Integer |
An unknown event, possibly a syntax error in an XML or JSON document |
||||
public | XML | String |
Value of documentType for XML |
Variable Summary
access | name | type | Can Read | Can Init | Can Write | Default Value | description |
---|---|---|---|---|---|---|---|
public-read protected | characterEncoding | String | subclass | subclass |
The character encoding as reported by the parser. The character encoding as reported by the parser. Will be null if not supported by the underlying parser. |
||
public-read protected | column | Integer | subclass | subclass |
The current column number in the source XML or JSON. The current column number in the source XML or JSON. Will be zero if not supported by the underlying parser. |
||
public | documentType | String |
Set this variable to specify the type of content to be handled by the parser. |
||||
public | encoding | String |
Set this variable to specify the character encoding of content to be handled by the parser. Set this variable to specify the character encoding of content to be handled by the parser. Setting this while a parse is in progress will reset the parser. The default encoding is specified by DEFAULT_ENCODING
See Also:
|
||||
public-read protected | event | Event | subclass | subclass |
The current parser event, which changes as the parser moves through the XML or JSON content. |
||
public | ignoreWhiteSpace | Boolean | false |
Set to true to ignore whitespaces and new lines for a TEXT event. |
|||
public | input | InputStream |
Set this variable to provide the parser with the source of content to parse. Set this variable to provide the parser with the source of content to parse. Setting this while a parse is in progress will reset the parser. It is the application's responsibility to close the stream when the parser is done. |
||||
public-read protected | line | Integer | subclass | subclass |
The current line number in the source XML or JSON. The current line number in the source XML or JSON. Will be zero if not supported by the underlying parser. |
||
public | onEvent | function(:Event):Void | null |
Callback, which if set to a non-null function, reports the current parse event to that function. |
Inherited Variables
Function Summary
- public forward(n: Integer) : PullParser
- public forward() : PullParser
- public parse() : Void
- public seek(element: java.lang.Object) : PullParser
-
Skip Events until the specified element is found.
Skip Events until the specified element is found.
-
Parameters
- element
- specifies the name of the XML element or JSON object to stop at. This needs to be a QName or String if documentType is XML or a String if documentType is JSON. The namespace is ignored if the element is a String and the documentType is XML.
- public seek(element: java.lang.Object, level: Integer) : PullParser
-
Skip Events until the specified element is found at the specified level.
Skip Events until the specified element is found at the specified level.
-
Parameters
- element
- a QName if documentType is XML or a String if documentType is JSON that specifies the name of the XML element or JSON object to stop at.
- level
- the depth at which the specified element must be found.
- public toString() : java.lang.String