Java Reference
In-Depth Information
The weather widget application is a simple example of how to process JSON
Web services, but what about XML? Consuming XML-based REST services is
completely supported, and the code only changes slightly. The two main differ-
ences are
•
Document type: Set to
PullParser.XML
(instead of
PullParser.JSON
).
•
Use of
javafx.data.xml.QName
: XML node names arrive via this object,
with namespace information, referenced from the
Event
object.
For instance, the code to parse the Yahoo! location data in XML form is shown
in Listing 10.11. The two differences listed in the preceding are highlighted in
the code in bold type.
Listing 10.11
Parsing XML
var locationInput: InputStream;
var locationParser = PullParser {
documentType:
PullParser.XML
;
input: bind locationInput
onEvent: function(event: Event) {
// parse the XML Yahoo data and
// populate the location object
if (event.type == PullParser.END_ELEMENT) {
if(
event.qname.name
== "City") {
location.city = event.text;
}else if (
event.qname.name
== "Latitude") {
location.lat = event.text;
}else if (
event.qname.name
== "Longitude") {
location.long = event.text;
}else if (
event.qname.name
== "State") {
location.state = event.text;
}
}
}
}
The
QName
class is part of the
javafx.data.xml
package that also contains the
XMLConstants
class. This class contains the following standard XML constant
strings:
XMLNS_ATTRIBUTE
. Example: “xmlns”
XMLNS_ATTRIBUTE_NS_URI
. Example: “http://www.w3.org/2000/xmlns/”
Search WWH ::
Custom Search