Java Reference
In-Depth Information
Listing 10.5
Using PullParser
class Location {
public var city: String;
public var state: String;
public var lat: String;
public var long: String;
}
var location: Location = Location{};
// ...
var parser = PullParser {
documentType: PullParser.JSON;
input: bind bis
onEvent: function(event: Event) {
// parse the JSON data and populate object
if(event.type == PullParser.END_VALUE) {
if(event.name == "City") {
location.city = event.text;
}
else if (event.name == "Latitude") {
location.lat = event.text;
}
else if (event.name == "Longitude") {
location.long = event.text;
}
else if (event.name == "State") {
location.state = event.text;
}
}
}
}
As PullParser progresses through the document, it fires a series of events (see
Table 10.1) that are handled by your code in the onEvent function.
As shown in Listing 10.5, each time a value is completely parsed, the onEvent
function compares the name to those that we're interested in. If there's a match,
the value is simply stored. In this case, as location specific data is received, the
code populates the Location object. With further use of object binding (which
was discussed in Chapter 4, Synchronize Data Models—Binding and Triggers),
it's easy to envision how individual UI elements will be updated to display the
location data—such as the city name—as the data is parsed in this code. In fact,
the sample JavaFX application we're going to explore in this section does just
that. Let's examine the complete sample application now, which shows the cur-
rent weather conditions for any valid ZIP code entered.
 
Search WWH ::




Custom Search