Oracle Blog

Tips and Tricks from Oracle's JDeveloper & ADF QA

XML Parsing in ADF Mobile

In this usecase, we have a web service that returns employee details(like name, designation, salary etc.) for an employee with given ID. In the mobile app, we will be calculating tax on
the returned salary and displaying it to the user in a pop-up.

To do this, we will be parsing the returned XML to fetch the
current salary and add a tax of 5% on it.

Here, we are assuming that tax is 5% of the current salary.

Pre-requisites:

Create a RESTful service as shown. We will be using this service at the mobile end to create the DC.

To create the service, first create a java class called Employee.java as shown below:

Name the connection ‘conn’
and suppy the target URL copied earlier. Remember to use IP instead of
localhost. Test the connection and click OK to close the wizard.

Since the service returns XML data, also create a Data
control. This will be useful for designing the UI page. To create DC, invoke
URL Service data control wizard in the ApplicationController project.

Test the URL connection in the last step and finish the
wizard.

Now, let us create the AMX pages. Open adfmf-feature.xml
present in ViewController project. Add a new feature ‘EmpFeature’ by clicking the plus icon as shown.

Drag and drop ‘id’
parameter from the DC palette onto the Panel Page of the Structure pane for
this page.

Drag and drop a button from the component palette onto the
page. Change the text of the button to ‘Get salary’.

As we want to invoke the service on click of this button,
let us associate an ActionListener with this. Focus on the button and from the
PI, select the arrow button for Action
Listener propery and click Edit
to bring up the below dialog. Click New to create a new managed bean.

Name the manged bean as
myBean and class as myClass.

Next, create a new method called calculateTax.

Go to myClass.java. In calculateTax method, write the code provided in the below table. Use the imports:

Lines 3 to 15 set various properties required to invoke the
web service. In lines 10 and 11, we are getting the value input by the user and
converting that to String inorder to pass the parameter to the service. In line 20,
we are creating a kXml parser instance. kXML is one of the core ADF Mobile libraries
that provides API that you can use to parse XML. As the parser needs a Reader
object, we are converting our services’ response into Reader in line 21 and
passing it in line 22.

To parse this response, we are setting the parser to the first tag
‘employee’ in line 24. In lines 25 through 42, we are going through the XML
elements. The comments are in-line for these lines. Finally, lines 43 through 47
are for displaying the pop-up. To display the pop-up, we are utilizing AdfmfContainerUtilities.invokeContainerJavaScriptFunction. For more
information, see link.

As mentioned in the article, we make use of the javascript
function which comes from phonegap-1.0.0.js. Hence, we need to include an entry
to this javascript file in our AMX page. Put the below entry in EmpPage.amx
above the panelPage.