Java Reference
In-Depth Information
F
Getting Zillow listings
—the third segment of the code shows the definition of the
function
getZListing(:Address)
. As you may expect, this function retrieves the
Zillow listing based on the address parameters passed to the service. Here is what is
going on inside the function:
First, we defined the URL string to the Zillow service. To keep things
simple, we use the global constants defined earlier to concatenate the
web service location, the Zillow API ID, and the address parameters.
Next, we instantiate a
PullParser
instance assigned to the
variable
parser
. JavaFX's
PullParser
is an event-based parser,
where an event cursor moves past the document as it is streamed
from its source. Each node of the document raises an event (see
There's more
next) that is handled by the event-handler function
attached to property
onEvent
of the parser.
In the code, the
onEvent
function takes a simplistic approach to processing
the node events. Basically, at the end of each element, detected with
if(event.type == PullParser.END_ELEMENT)
, the code tests all
expected element values and grabs the element that matches the expected
name using a chain of
if
expressions.
The next portion of the
getZListing(:Address)
function
is the declaration of a
HttpRequest
instance assigned to the
variable
http
. This is used to communicate with the server and
retrieve the XML document. The event-handler function assigned
to the
http.onInput
property connects the XML document's
InputStream
to the
PullParser.in
property. The code then
calls the
PullParser.parse()
function to kickoff the parsing of
the document.
F
Getting the Google Map image
—retrieving the Google Map image is done by
the function
getGMap(:Address)
. It uses the same technique covered in the
recipe
Downloading images with HttpRequest
. It uses an instance of
HttpRequest
to download the binary data of the image and assign it to an instance of
ImageView.image
.
There's more...
JavaFX's
PullParser
class is designed to handle both XML and JSON structured data
formats. The
PullParser
API uses a convention based on the Document-Object-Model
(DOM) to identify the nodes in the document. As the parser encounters a document node
during traversal, it generates a pull event and calls a user-defined event handler specified
through parser property
onEvent:function(:javafx.data.pull.Event)
. Both
XML and JSON documents are handled in the same manner. The following table lists some
common pull events generated by the parser: