Vyoms
Bookmark and Share Rss Feeds

Click here to register on Shine.com - India's Fastest growing Job site!
JavaFX HTTP Networking and XML Parsing | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Hot Jobs
leftMenu Bullet Freshers Jobs
leftMenu Bullet Experienced Jobs
leftMenu Bullet Government Jobs
leftMenu Bullet Walkin Jobs
Placement Section
leftMenu Bullet Company Profiles
leftMenu Bullet Interview Questions
leftMenu Bullet Placement Papers
Interview Ebook
Get 9,000+ Interview Questions & Answers in an eBook.
Interview Questions & Answers Kit
  • 9,000+ Interview Questions
  • All Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades
Resources @ VYOMS
leftMenu Bullet Companies In India
leftMenu Bullet Consultants In India
leftMenu Bullet Colleges In India
leftMenu Bullet Exams In India
leftMenu Bullet Latest Results
leftMenu Bullet Notifications In India
leftMenu Bullet Call Centers In India
leftMenu Bullet Training Institutes In India
leftMenu Bullet Job Communities In India
leftMenu Bullet Courses In India
leftMenu Bullet Jobs by Keyskills
leftMenu Bullet Jobs by Functional Areas
Learn @ VYOMS
leftMenu Bullet GATE Preparation
leftMenu Bullet GRE Preparation
leftMenu Bullet GMAT Preparation
IAS Preparation
leftMenu Bullet SAP Preparation
leftMenu Bullet Testing Preparation
leftMenu Bullet MBA Preparation
News @ VYOMS
leftMenu Bullet Freshers News
leftMenu Bullet Job Articles
leftMenu Bullet Latest News

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » JavaFX HTTP Networking and XML Parsing

JavaFX HTTP Networking and XML Parsing



Search Jobs:
(For ex: Software Testing Jobs, Java Jobs, .Net Jobs)
 


Article Posted On Date : Wednesday, May 20, 2009


JavaFX HTTP Networking and XML Parsing
Advertisements

HTML clipboard

The recently released JavaFX platform allows developers to build rich internet applications (RIA) that can include audio and video. Using JavaFX, it is possible to create highly interactive applications. Moreover, it is possible to easily create content for different devices (desktop, mobile phone, television, and so on). JavaFX is a compiled language, like Java, and is highly portable and is based on the familiar paradigm "Write Once, Run Everywhere."

JavaFX is focused on the client side, and aims to improve the look and feel of Java GUIs so that users can experience more attractive interfaces. Of course, many client applications need to exchange information with a remote server. Nowadays, the HTTP protocol and XML are widely accepted as the best choices to exchange information, so we want to show how easy is in JavaFX to handle HTTP communication details and how we can parse and extract information from an XML data structure.

In the article we will assume you are already familiar with the basic notions of the JavaFX language.

JavaFX Basic Language Concepts

While it is a compiled language, JavaFX mixes the features of scripting languages with those inherited from Java. Scripting languages allow for fast and easy application development, while JavaFX's Java-based heritage allow it to be a robust language.

JavaFX proposes a new coding paradigm: as a declarative language, it compels us to describe how we want our application to behave without describing the specific control flow, as we do with imperative languages. This paradigm is really powerful when we need to develop GUIs. The basic idea that stands behind that JavaFX GUI development model is that you "describe" what your interface should look like. There is a strict relationship between the code and the "visual structure." Moreover, the order used to declare objects, in the code, reflects the order used to display them. The overall result is an elegant way to create a GUI in fewer lines of code; this makes applications easier to understand and maintain.

Another interesting feature of JavaFX is that it is a statically typed language, meaning the data type of every variable, function, and so on is known at compile-time. See the Resources section for links to JavaFX tutorials that explore this trait further.

JavaFX HTTP and XML Package Overview

To develop an application using HTTP protocol and XML, JavaFX provides several packages, which are shown below:

  • javafx.io.http for handling HTTP communication
  • javafx.data.pull and javafx.data.xml for XML parsing

The class diagram in Figure 1 shows the classes contained in these packages.

HTTP and XML packages and classes
Figure 1. Defining the channel rule for the RDBMS Event Generator (click for larger view)

HTTP and JavaFX

To handle the HTTP protocol, we can use HttpRequest class in the javafx.io.http package. This class makes asynchronous HTTP requests to a remote server that supports the HTTP protocol. The HTTP methods currently supported are:

  • GET
  • POST
  • PUT
  • DELETE

This class is neutral in respect to the data format exchanged, so we can invoke a remote server and send whatever type of information we like, as long as we supply an OutputStream containing the data that must be sent, using the POST or PUT HTTP methods.

The HttpRequest operation, related to each HTTP method supported, has a specific lifecycle. We focus our attention on the lifecycle in the case of HTTP GET method; for other methods (POST, PUT, DELETE), the lifecycle is very similar. In the case of an HTTP GET request, the lifecycle is shown in Figure 2.

HTTP GET method request life-cycle
Figure 2: HTTP GET method request lifecycle (click for larger view)

As we can see from the diagram above, each state of the lifecycle is defined by a specific value of the internal variables of the HttpRequest class. Related to each variable transition, there is a corresponding method that is called during the transition itself, so that we can control and handle different states in the HTTP lifecycle. These methods have the same name of the corresponding variable, prepended with on. For example, if we want to track when the request is trying to connect to the server, we will use the onConnecting function.

It is time we start coding our JavaFX HTTP client. First of all we have to declare a variable that contains our URL:

  def url : String = "http://www.java.net";    

Then we create the HTTP request and define our callback function, which is called when the HTTP request starts connecting.

  HttpRequest {          location: url;          onConnecting: function() {            java.lang.System.out.println("Connecting");          }  }.enqueue();    

Notice the method enqueue() that makes the request.

Now we want to read the response body. We can do that using the InputStream provided by the function onInput. We need to add this piece of code to our client.

      onInput: function(is: InputStream) {        try {          var responseSize : Integer = is.available();          java.lang.System.out.println("Response size {responseSize}");        }        finally {          is.close();        }      }    

The last step is to handle any exceptions that can occur during the HTTP request. The HTTPRequest has a function that is called whenever an exception occurs. So we can add the exception-handling code below to our client.

      onException: function(ex : Exception) {        System.out.println("Error: {ex.getMessage()}");      }    

If you run the client using NetBeans, you should see output similar to Figure 3:

Client log

Figure 3: Client log

In the package javafx.io.http, there are two other classes called HttpHeaders and HttpStatus. The first class defines a set of constants that map the corresponding HTTP header value names. The second class defines a set of constants corresponding to the possible HTTP response codes.

XML API

As we said, many clients today send data over HTTP using an XML format, and JavaFX offers the capability to easily parse an XML document. We focus our attention now on the other two packages, shown before in Figure 1:

  • javafx.data.xml
  • javafx.data.pull

The package javafx.data.pull contains the classes to parse an XML document, while the javafx.data.xml package defines some constants and handles qualified names. The parser is event-based (similar to the SAX parser) and it supports two different data formats:

  • XML
  • JSON

For this article, we'll focus our attention on the XML data format.

The PullParser class, the heart of JavaFX's document parser, accepts several attributes that can be used to control the parser. First of all, we need to declare the document type we want to parse, which we do by using the class attribute documentType. This string can have two values:

  • PullParser.XML is used for parsing XML
  • PullParser.JSON is used for parsing JSON

After we declare the document type, we need to supply the input document to parse. The parser accepts an input stream, and as we will see later, this is very handy when we need to parse an XML document retrieved from an HTTP request. To declare the input stream we need to set the value of the input variable.

So it is time we create an instance of our PullParser, as shown below:

   parser = PullParser {    documentType: PullParser.XML;    input: xmlFileInputStream;  }    

While the parser analyzes the document, it generates a set of events. We need to implement a callback function to be called in response to these events. The callback function is called onEvent and in its body, we implement our logic to extract information from the document, which we will do later.

The function signature is onEvent(event : Event), where the Event class belongs to the package javafx.data.pull. This class contains all the information related to the pull-parsing event, and we can use it to extract the information we need. The type declares the type of event, as one of the values defined in PullParser. We are interested in the following types of events:

  • START_DOCUMENT: This event is generated at the beginning of document parsing.
  • START_ELEMENT: This event is generated when the parser finds a new starting element. We can use this event to read the element attribute.
  • END_ELEMENT: This event is generated when the parser finds the end of the element. We can use it to read the text contained in the element.
  • END_DOCUMENT: This event is generated when the parser reaches the end of the document.

There are other events that can be used for JSON documents; if you're interested, have a look at the PullParser documentation. At any rate, here's an onEvent skeleton implementation to react to the START_ELEMENT and END_ELEMENT events.

    onEvent: function(event : Event) {          /* We start analyzing the different event types */          if (event.type == PullParser.START_ELEMENT) {            /* Here we implement our logic to handle the start element event,               for example to extract the attribute values and so on */          }          else if (event.type == PullParser.END_ELEMENT) {            /* Here we implement our logic to handle the end element */          }    }    

During the parsing process, some errors can occur. We can manage them verifying the type of Event generated by the parser.

Integrating the HTTP and XML APIs

Now that we have described these two APIs, it is time we look at the most interesting part: how we can integrate everything so that we can code a complete XML-over-HTTP client. This can be useful if we want to have a client that exchanges information with a remote server.

Let's suppose that our JavaFX client application invokes a servlet that returns an XML file with the structure shown below:

  <?xml version="1.0" encoding="UTF-8"?>  <data>    <person id="1">          <name>Mikey</name>          <surname>Mouse</surname>    </person>  </data>    

This is a simple XML file, but it is enough for the purpose of our example. Our goal is for our client to connect to the test servlet and retrieve the XML content, and then parse it and show the extracted information. To do that, we need to change the HttpRequest function onInput so that when we start receiving the XML document we parse it, too. The code below shows how to do it:

    onInput: function(is: InputStream) {      try {                      PullParser {          input: is;          onEvent: function (event : Event) {                                         // We handle the event                  }            }.parse();      }      finally {        is.close();      }       }         

Notice how we have added the PullParser to the onInput function, and that we set the parser input stream to the one received from the HttpRequest. Now we just need to handle the events as we described before:

  ....      if (event.type == PullParser.START_ELEMENT and event.level == 1) {        java.lang.System.out.println("Start a new element {event.qname.name}");        var qAttr : QName = QName {name : "id"};        var attVal : String = event.getAttributeValue(qAttr);        java.lang.System.out.println("Attribute ID value {attVal}");      }      else if (event.type == PullParser.END_ELEMENT) {        var nodeName : String = event.qname.name;        java.lang.System.out.println("End element {nodeName}");        // Now we extract the text only if the node is name or surname        if (nodeName == "name" or nodeName == "surname") {          var textVal : String = event.text;          java.lang.System.out.println("Text {textVal}");        }      }    ....    

It is useful to analyze the code step by step. In the case of a PullParser.START_ELEMENT event, we use the event.level variable. This tells us at which line the event occurs (starting from zero, the XML document root). We know already that the id attribute is present only on the first line, so we limit the extraction to this line only. Then we create a QName object setting, the name variable to our attribute name, and then we extract the value.

In the case of PullParser.END_ELEMENT, we want to extract the node content. To do this, we use the text variable that contains the node value.

If everything works properly we will see the parsed items in the console, as shown in Figure 4.

HTTP request with XML parsing

Figure 4. HTTP request with XML parsing

Conclusion

In this article, we explored some essential features of JavaFX, focusing our attention on two important aspects: XML and HTTP. We discovered how easy is to develop a simple client that makes an HTTP request and parses the XML response. This is a basic example, but it can be further expanded adding other features; for example, connecting to a site and retrieving pictures.



Latest News Alerts
A diabetes breakthrough from India
A team of Indian scientists has discovered a novel form of insulin that could drastically reduce the suffering diabetics face in controlling their blood sugar. For the diabetics, daily painful pinpricks to inject doses of insulin is a routine affair, now in a new discovery scientists claim a single shot of insulin [...]
TamilNadu Open University (TNOU) Distance Education B.ED. Admission 2010
Tamil Nadu Open University (TNOU) ADMISSION NOTIFICATION FOR B.Ed 2010-11 Tamil Nadu Open University Dote Campus Chennai 60025 Website: www.tnou.ac.in B.Ed/B.Ed Special Education (Tamil/English medium) TNOU invites application for admission to Bachelor of Education (B.Ed) (recognized by govt of Tamilnadu, NCTE/DEC) Bachelor of Education in Special Education (B.Ed SE) (recognized by govt of Tamilnadu/RCI, New Delhi) For eligibility [...]
Yeddyurappa rejects CBI probe into illegal mining issue
Even after the high-drama helmet protest by Opposition MLAs, Karnataka CM BS Yeddyurappa on Tuesday rejected the demand of CBI probe into the illegal mining issue, according to reports. Earlier on Monday, raising the pitch on the illegal mining scam, opposition Congress and JD(S) MLAs and MLCs spent the entire night [...]
Cash-less hospitalisation scrapped, patients hit
Insurance companies are scrapping cash-less hospitalisation across the country which is going to affect those in need of medical treatment. Five days ago 34-year-old Nandita was admitted to a hospital with severe anemia. Three days later she has shelled out Rs 70,000 rupees for treatment in cash as despite paying for cashless hospitalisation [...]
KVPY 2010 Kishore Vaigyanik Protsahan Yojana
Applications are invited for KVPY Fellowships for school and college students interested in research careers The department of science and technology, govt of India, offers attractive fellowships (Rs 4000 to Rs 7000 p.m.) and contingency grants (equivalent of four months fellowships per annum) to students studying in XI standard to B.Sc/B.S./Integrated M.Sc [...]
Evening storm lashes Delhi, kills 11
The Capital received the heaviest rainfall of the season on Monday evening. But the respite from three days of heat and sweat was also mired by reports of 11 deaths from across the Capital after the downpour. Six people — two at Dariba Kalan in Chandni Chowk and one at Mori Gate [...]
MS Dhoni signs Rs 200-cr endorsement deal
Team India captain MS Dhoni has signed a 200 crore rupees endorsement deal with a talent management company, taking him past Sachin Tendulkar who earlier held the contract crown. The current deal with a joint venture company, Rhiti Sports Management and Mindscapes One, is for three years. They will manage the Indian skipper’s endorsements [...]
Infosys Profit Unexpectedly Falls After Cut in Prices, Increase in Taxes
Infosys Technologies Ltd., India’s second-largest software exporter, reported profit fell during the first quarter after it cut prices to retain contracts and paid higher taxes. Net income fell 2.6 percent to 14.9 billion rupees ($318 million) in the quarter ended June 30, from 15.3 billion rupees a year earlier, after income taxes [...]


SPONSORS

FREE JOBS NEWSLETTER
3,11,757 [96,218 + 2,15,539] MEMBERS!


Contact Us | Feedback | Link to Us
Copyright © 2001-2009 VYOMS.com. All Rights Reserved. Home | About Us | Jobs | Contact Us | Privacy Policy | Terms & Conditions.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.
Placement Papers | FREE SMS | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2009