BlackBerry: Working with XML

XML is one of the most commonly used data-interchange formats on the web. With the proliferation of smartphones, it has become a common requirement to parse and display XML documents on mobile devices. In this article, I'll teach you how to parse an XML document on BlackBerry and also display an XML document on BlackBerry.


Installing the BlackBerry Eclipse Plugin

First, we need to install the BlackBerry Java Plug-in for Eclipse, which may be installed with one of the following two methods.

  1. From the BlackBerry Java Plugin Update site
  2. From the plugin .exe file BlackBerry_JDE_PluginFull_1.5.0_helios.exe.

We shall discuss each of these methods in this section. To install from the update site, select Help>Install New Software in Eclipse Java IDE. In the Install window, click on Add underneath Available Software. In the Add Repository window, specify the Name as BlackBerry Java Plug-in Update Site and the Location as http://www.blackberry.com/go/eclipseUpdate/3.6/java as shown in the following illustration.



Specifying BlackBerry Plugin Update Site Location

The update website gets added to Available Software and the available software for the website gets listed including the different versions. Select BlackBerry Java Plug-in Category Version 7.0.0.28. Click on Next.



Selecting Software to Install

In the Install Details section, the BlackBerry Java SD 7.0.0.28 gets listed. Click on Next.



BlackBerry Plugin Install Details

Accept the license agreement and click on Finish. The Installing Software dialog gets displayed. After the software gets installed a dialog to indicate the Restart requirement gets displayed. Click on Restart Now. The BlackBerry Java Plug-in for Eclipse gets installed.

In the second method, click on the BlackBerry_JDE_PluginFull_1.5.0_helios.exe or an earlier version exe file. The BlackBerry Java Plug-in for Eclipse wizard gets started. Click on Next in Introduction.



The BlackBerry Java Plug-in for Eclipse wizard

Accept the license terms and click on Next. In Choose Installation Folder specify the directory in which to install the plugin, including the Eclipse IDE. Click on Next.



Selecting Installation Folder

In Select Additional Tasks select the default shortcut tasks and click on Next. In Pre-Installation Summary click on Install.



Installing BlackBerry Plugin

The BlackBerry Java Plug-in for Eclipse gets installed. Click on Done. Next, we need to open the BlackBerry perspective. Select Window>Open Perspective>Other. In Open Perspective, select BlackBerry Application Development and click on OK.


Opening the BlackBerry Application Development Perspective

Creating a BlackBerry Project

In this section we shall create a BlackBerry project in Eclipse using the BlackBerry Application Development perspective features. Select File>New. In New window select BlackBerry>BlackBerry Project and click on Next.



Creating a BlackBerry Project

In the New BlackBerry Project window, specify Project Name (ParsingXML for example). Select JRE as BlackBerry JRE 7.0.0. Click on Next.



Specifying BlackBerry Project Name

In Java Settings select the default build settings, which include src and res folders for project source code and the bin folder for the output. Click on Next.



Specifying BlackBerry Project Java Settings

In the Templates section, select the BlackBerry Application template and click on Next.



Selecting Project Template

In the UI Application window, specify the Application Details. Specify a Package Name (blackberry.xml), Application Class Name (ParsingXML), Screen Class Name (Parsing_XML), and a Screen Title. Click on Finish.



Specifying Application Details

A BlackBerry Project gets created. The BlackBerry project consists of the following
artifacts:

  1. A class (ParsingXML) that extends the UiApplication class.
  2. A class (Parsing_XML) that extends the MainScreen class.
  3. A BlackBerry application descriptor file BlackBerry_App_Descriptor.xml.

The directory structure of the BlackBerry project is shown below.



BlackBerry Project Structure

Configuring the BlackBerry Descriptor

The properties of a BlackBerry project are specified in the BlackBerry_App_Descriptor.xml file, shown below in GUI mode.



BlackBerry_App_Descriptor.xml file

The General Information includes the following property fields/checkboxes.

Field Description Value
Title The application title Parsing XML
Version The application version 1.0.0
Vendor The vendor name BlackBerry Developer
Application Type "BlackBerry Application" for a BlackBerry application BlackBerry Application
BlackBerry Application Runtime arguments to the application
Auto-run on Startup Select checkbox to auto-run application on startup true
Startup Tier The startup priority 7 (default) is the lowest priority
Do not display the application icon Select checkbox to not display application icon

The Application Descriptor may also include Locale Resources and Application Icons. Alternate entry points may be used to run an application in background or foreground. The BlackBerry_App_Descriptor.xml is listed below.


Creating the Screen class

In this section we shall create a screen for the BlackBerry application. At the minimum a screen should have a title. A screen is a MainScreen class and provides features common to RIM device applications. Create a class that extends the MainScreen class and in the class constructor set the screen title using the setTitle method. The screen class Parsing_XML is listed below.


Parsing an XML Document

In the ParsingXML class we shall parse an XML document. The catalog.xml document has a catalog for a journal.

Copy the XML document to the res folder in the src folder, or directly in the src folder. Next, we shall develop the ParsingXML class, which extends the UiApplication class, the base class for all device applications that provide a user interface. Create an instance variable for the MainScreen. Create a String variable for element values. We shall use a Thread to parse the XML document. Create an instance variable for the thread class.

In the class constructor set the screen title, and add the MainScreen screen onto the UI stack for rendering. Create a Thread instance and start the thread with the start() method.

In the main method, create an instance of the ParsingXML class and invoke the event dispatcher thread using the enterEventDispatcher method.

All BlackBerry framework UI applications contain one event dispatcher thread. After a screen has been pushed onto the display stack all modifications must be made either on the event dispatcher thread or a background thread. We shall use a background thread Connection to parse the XML document catalog.xml.

Create a DocumentBuilderFactory factory instance using static method newInstance() to create a DOM parser from and subsequently create a DOM tree from the XML document.

Create a DocumentBuilder parser instance using the newDocumentBuilder method to subsequently parse the XML document and create a DOM document object.

Set the parser to be validating using the isValidating() method.

Create an InputStream object from the catalog.xml document. Include the '/' in the catalog.xml path.

Alternatively, the InputStream may be created using a String literal for the XML, and a ByteArrayInputStream.

Next, obtain a DOM Document object from the InputStream using the parse(InputStream) method.

Get the Document element, which is the child node of the XML document, also the root element, using the getDocumentElement() method. Normalize the XML document using the normalize() method, which removes adjacent and empty Text nodes.

We shall output the <title> element values to the screen. Create a NodeList of the title elements using the getElementsByTagName() method.

The first child node of an element is the text node in the element. The text value in a Text node is obtained using the getNodeValue() method. Iterate over the NodeList and obtain the text node values for the title elements. To display the title values on the screen first, we need to obtain the event lock using the getEventLock() method.

The event lock is required for performance and concurrency issues. Output the title values to the screen using the add() method.

If using the String literal for the XML document output the String to the screen.

In the catch block output exception message if any.

The ParsingXML.java class is listed below.


Running the BlackBerry Project

Next, we shall run the ParsingXML application in Eclipse IDE. Right-click on ParsingXML and select Run As>BlackBerry Simulator.



Running BlackBerry Application

The BlackBerry Simulator gets started. The application gets installed on the BlackBerry Simulator.



Installed BlackBerry Application

Click on the application to run the application.



Running BlackBerry Application

The titles from the catalog.xml document get displayed.



Parsing XML Document

The directory structure of the ParsingXML application is shown below.



Directory Structure of ParsingXML Application

If the XML document is created from a String literal, the XML may be output to the screen as well.



Displaying XML Document



Summary

In this article we learned about parsing XML on a BlackBerry using the BlackBerry JDE. We used the BlackBerry Java Plug-in for Eclipse to add the BlackBerry Application Development perspective to Eclipse IDE. We used the MainScreen class to create a screen for BlackBerry device. We used the UiApplication class to create an event dispatcher thread, which a BlackBerry framework application is required to have. We created a background thread to parse an XML document and obtained an event lock to display the parsed catalog titles on the BlackBerry screen. We also output the XML document to the screen.

Tags:

Comments

Related Articles