Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

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
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

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








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.






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
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.