You Are Here:

Community: Wiki

This page was last modified on 3 August 2009, at 11:25.

WidSets for Intermediate EP 2 : HTTP with XML Filter

From Forum Nokia Wiki

This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. The article is believed to be still valid for the original topic scope.

Contents

Introduction

In WidSets for Intermediate EP 1 : HTTP Request, I showed you how to download data from web server via http request. This page, I will show you how to work with data in XML file that has been downloaded via http.

XML Filter

XML is standard file format that is very popular for data exchange over the internet, for example, RSS feed. So, some develop may need to display data in these XML files on his/her widget. How to do that? ... You have 2 choices.

  1. Download XML file via HTTP request and parse it yourself.
  2. Download XML file via HTTP request and let XML Filter parse it for you.

Sure! I suggest you to select the second choice! Let’s see how it works.

Data Flow Diagram

In normal HTTP request, downloaded content will be sent to onSuccess callback function directly (in this example, it's ok(...) function).

Image:WidSetsNormalHTTPDTD.png

But for HTTP request that cooperate with XML filter, downloaded content will be parsed by XML filter first and then the parsed data will be sent to onSuccess function afterward in the Value form.

Image:WidSetsXMLFilterDTD.png

Writing XML Filter

XML Filter is quite complicate. So let’s learn by example. Here is the sample XML file.

data.xml

<?xml version="1.0" encoding="utf-8"?>
<userdata>
<name>Sittiphol Phanvilai</name>
<namethai>สิทธิพล พรรณวิไล</namethai>
<address>
<city>Bangkok</city>
<state>-</state>
<country>Thailand</country>
</address>
<pet type="Dog" name="Richie"/>
</userdata>

How to get data inside these tags? You have to modify widget.xml and add XML filter to parse this XML.

widget.xml

To use HTTP with XML filter, you have to add http service with filter tag in widget.xml.

 <services>
   <service type="http" id="httpService">
     <filter id="digg"/>
   </service>
 </services>

Next, write the XML filter in widget.xml with the name declared in service tag.

<filters>
<filter id="digg">
<list>
<item name="name">
<xpath>/userdata/name/text()</xpath>
</item>
 
<item name="namethai">
<xpath>/userdata/namethai/text()</xpath>
</item>
 
<item name="address">
<choice>
<xpath>/userdata/address/province/text()</xpath>
<xpath>/userdata/address/country/text()</xpath>
<xpath>/userdata/address/city/text()</xpath>
</choice>
</item>
 
<item name="pet">
<strcat>
<xpath>/userdata/pet/@name</xpath>
<str> (</str>
<xpath>/userdata/pet/@type</xpath>
<str>)</str>
</strcat>
</item>
</list>
</filter>
</filters>

httpxmlfilter.he

Write data fetching using HTTP request using call function.

 void fetchxmldata()
 {
   String URL = "http://www.nuuneoi.com/neoi/widsets/httpxmlfilter/data.xml";
   
   Prompt prompt = new Prompt(null, "Loading...", null, null);
   prompt.push();
   
   //widsets http service parameters
   Value arg = [
     "url" => URL
   ];
   call(null, "httpService", "get", arg, ok, nok);
   
   void ok(Object state, Value ret)
   {
     prompt.pop();
     prompt = null;
     flow.clear();
     foreach (Value item : ret) {
       String key = item[0]; 
       String value = item[1]; 
       {
         Label label = new Label(getStyle("labelKey"), key);
         label.setPreferredWidth(-100);
         label.setFlags(VISIBLE|LINEFEED);
         flow.add(label);
       }
       {
         Label label = new Label(getStyle("labelValue"), value);
         label.setPreferredWidth(-80);
         label.setFlags(VISIBLE|LINEFEED);
         flow.add(label);
       }
     }
     setBubble(null, "Completed");
   }
   
   void nok(Object state, String error)
   {
     prompt.pop();
     prompt = null;
     setBubble(null, "HTTP POST Failed: " + error);
   }
 }

When data get ready, function ok will be called with parsed data contain in ret. You can use foreach to get these value and use them.

Result

Image:WidSetsHTTPXMLExample01.png

Filter Explanation

To get data inside tag, filter has to be written in this form.

     <item name="KEY_NAME">
       <xpath>/path/to/data/text()</xpath>
     </item>

To get parameter value inside tag, filter has to be written in this form.

     <item name="KEY_NAME">
       <xpath>/path/to/tag/@parametername</xpath>
     </item>

For example:

     <item name="name">
       <xpath>/userdata/name/text()</xpath>
     </item>

Here is data that has been sent to onSuccess function.

  • key = name
  • value = /userdata/name/text() which mean data in <userdata><name>...</name></userdata>

Filter can be written in many ways, <choice> tag can be applied to make more advance filter. <choice> will select first data of XML nodes listed inside that is not null.

     <item name="address">
       <choice>
         <xpath>/userdata/address/province/text()</xpath>
         <xpath>/userdata/address/country/text()</xpath>
         <xpath>/userdata/address/city/text()</xpath>
       </choice>
     </item>
  • key = address
  • value = first data of XML node listed below that is not null
    • <userdata><address><province>...</province></address></userdata>
    • <userdata><address><country>...</country></address></userdata>
    • <userdata><address><city>...</city></address></userdata>

You also can format string using <strcat> tag. All strings inside will be concatenated.

     <item name="pet">
       <strcat>
         <xpath>/userdata/pet/@name</xpath>
         <str>(</str>
         <xpath>/userdata/pet/@type</xpath>
         <str>)</str>
       </strcat>
     </item>
  • key = pet
  • value will be formatted in @name(@type) .
    • /userdata/pet/@name means parameter value name in <userdata><pet ...> tag

These are basic of XML Filter. If you need more advance XML filter, you can read at Advanced_filters.

Character Encoding

You may already notice that value in field namethai is not like one in XML. It happened because of character encoding problem. Data come in UTF-8 format but they are shown on screen using ASCII format. To solve this, you have to transcode data from ISO-8859-1 to UTF-8 using decodeString and encodeString method like this.

 String key = decodeString(encodeString(item[0], "ISO-8859-1"), "UTF-8"); 
 String value = decodeString(encodeString(item[1], "ISO-8859-1"), "UTF-8");

Result

Image:WidSetsHTTPXMLExample02.png

Code Snippet

You can download source code for this tutorial from File:WidSets HTTP with XML Filter Example.zip

See Also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia