Mar
06
2010

Flash Conditional Compilations in CS4

Flash CS4 has features can be used to define variables for conditional compilations.  It is useful to use one code base for multiple versions or to tag part of code to compile only under some build. The dialog is avaliable at: File->Publish Settings.  In the Publish Settings dialog, select the Flash tab and then click the Settings button. az1More guide about how to use the conditional conpilation variables is in the following page: http://permadi.com/blog/2009/04/flex-builder-conditional-compilation/
Mar
06
2010

Integrating/Reading RSS Feed Using PHP

There are built in library to do RSS Feeds, but won't it be nice to build our own and be able to customize the output.  PHP 5 or later is needed for this method because it uses SimpleXML. Below are some useful links that we use in this tutorial.

Reading The RSS File

Using SimpleXML, the code below read the RSS file and outputs the XML structure.  This example does not do much but it demonstrates that the file is being read and parsed.
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo var_dump($xmlData);;
?>
The result is below:

Displaying The Data

For starter, let'd siaplay the title, copyright, and the date the RSS file is generated. The following code reads the file, then displays the title, lastBuildDate, and copyright.
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo "
<h2>".$xmlData->channel->title."</h2>
";
echo "
<h3>Updated at ".$xmlData->channel->lastBuildDate."</h3>
";
echo "Copyright ".$xmlData->channel->copyright."";
?>
The result is below:
As you see, it's very straightforward.

Displaying The Logo

There's an image node in the RSS, let's display it.
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo '<img src="'.$xmlData->channel->image->url.
 '" width='.$xmlData->channel->image->width.
 '" height='.$xmlData->channel->image->height.' ></img>';
?>

Displaying The News Items

If you examine the XML structure, you can see that the items are within the channel node. To display the item, we can iterate all of them. Below is an example of showing the title of each item title.
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo '<img src="'.$xmlData->channel->image->url.
 '" width='.$xmlData->channel->image->width.
 '" height='.$xmlData->channel->image->height.' ></img>';
echo "
";
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
 echo "Item ".$i.":".$xmlData->channel->item[$i]-><strong>title</strong>."
";
}
?>

Linking to the News Item

Linking can be done by making each item a link, using link node. And while at it, let's display the pubDate (publication date) too.
<?php
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
 $link=$xmlData->channel->item[$i]-><strong>link</strong>;
 $title=$xmlData->channel->item[$i]->title;
 $publicationDate=$xmlData->channel->item[$i]-><strong>pubDate</strong>;
 echo "
<h2><a href='".$link."'>".$title."</a></h2>
";
 echo "
<h3>".$publicationDate."</h3>
<hr>";
}
?>

Formating Nicely

Now that you know the basic, the formatting can be made nicer. Here's an example:
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo '<img src="'.$xmlData->channel->image->url.
 '" width='.$xmlData->channel->image->width.
 '" height='.$xmlData->channel->image->height.' ></img>';
echo "
";
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
 $link=$xmlData->channel->item[$i]->link;
 $title=$xmlData->channel->item[$i]->title;
 $publicationDate=$xmlData->channel->item[$i]->pubDate;
 echo "
<div class='newsItem'><a href='".$link."'>".$title."</a>";
 echo "
<div class='pubDate'>".$publicationDate."</div>
</div>
";
}
?>
Result:
And that concludes this tutorial. It's quite straightforward, isn't it? PS: remember to check the RSS owner's usage terms before using them. Note: I am not in any way related to BusinessWeek, I am using their RSS only for illustration purpose.
Mar
02
2010

Android Home Screen Replacement: Home++

This is curently in beta and has nice layout where the bottom of the screen contains a set of commonly used task icons, a calendar on the top, and flippable orientation when in horizontal position.  http://hpp.intuitit.mobi/ .  Screenshots are from Nexus One.
homeplusplus

homeplusplus21 homeplusplus3
Feb
25
2010

Running Android SDK Examples Using Eclipse

The Android SDK comes with several very nice examples.  Here’s a short guide on how to compile and run them.  This tutorial is intended for Eclipse begginners who are already familiar with progamming.

This assumes you have Android SDK and Eclipse already set-up, following this previous tutorial.

  1. Start Eclipse.  If you see the welcome screen, like below, close it (using the X icon next to the word Welcome).
    az40
  2. Select this menu: Window->Open Perspective->Other.  A Window like below should appear.  Select Java (default) since Java is the language used by Android SDK.
    az63
  3. Select this menu: File->New -> Project, a dialog like below should appear, then select Android->Android Project.
    az58
  4. This dialog will open.  This is the place to create a new project, but in this tutorial, we are not going to create a new project.  Instead, we will be importing one of the Android example projects.
    az60
    If you don’t see Android 2.1 listed, check the previous tutorial.  Select Create project from existing sample in the Contents section.
  5. Now select which sample you want to compile.  For this example, let’s chose Lunar Lander.  If it doesn’t allow you to select a Sample project, make sure to click/click the checkbox under tge the Build Target section again.
    az60-copy
  6. The project name is automatically filled for you, but you can change it if you want.

  7. Next, you can click Finish.
  8. The project should be created and you should see the Project name on the Package Explorer Window.  You can expand the list by clicking the rectangle.  Files should be listed like below.
    az64If you don’t see the Package Explorer window, go to menu: Window->Show View->Package Explorer.
  9. Select Run->Run Configurations… Select the Target tab.Check and make sure the Virtual Device you created in the previous  tutorial is listed.  This device is an emolator that will run the application.  I suggest selecting Manual so that you can select devices before the app runs.
    az67
  10. Select menu Run->Run.  A window like below should appear.  Select Android Application, then click OK.  The application should start
    az66
    If you have Manual set in the Run Configuration, the you might see a window like below.  Don’t worry if you don’t see as many devices in the Virtual Device desction.  If you have an actual phone hooked up, you should be able to select it too in the Choose a running Android device.  Select a Virtual Device or an actual device.

    az68

  11. For some reason, the Android simulator is slow to start.  Pay attention to the Console window in Eclipse (Window->Show View->Console), you should not be getting any error.  The log may be similar to below:
    [2010-02-25 22:03:37 - LunarLander]——————————
    [2010-02-25 22:03:37 - LunarLander]Android Launch!
    [2010-02-25 22:03:37 - LunarLander]adb is running normally.
    [2010-02-25 22:03:37 - LunarLander]Performing com.example.android.lunarlander.LunarLander activity launch
    [2010-02-25 22:03:37 - LunarLander]Automatic Target Mode: launching new emulator with compatible AVD ‘FPTestDevice’
    [2010-02-25 22:03:37 - LunarLander]Launching a new emulator with Virtual Device ‘FPTestDevice’
    [2010-02-25 22:03:39 - Emulator]### WARNING: SD Card files must be at least 8 MB, ignoring ‘/Users/ferrypermadi/.android/avd/FPTestDevice.avd/sdcard.img’
    [2010-02-25 22:03:43 - LunarLander]New emulator found: emulator-5554
    [2010-02-25 22:03:43 - LunarLander]Waiting for HOME (‘android.process.acore’) to be launched…
    [2010-02-25 22:03:44 - Emulator]2010-02-25 22:03:44.422 emulator[28109:903] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
    [2010-02-25 22:04:22 - LunarLander]WARNING: Application does not specify an API level requirement!
    [2010-02-25 22:04:22 - LunarLander]Device API version is 7 (Android 2.1)
    [2010-02-25 22:04:22 - LunarLander]HOME is up on device ‘emulator-5554′
    [2010-02-25 22:04:22 - LunarLander]Uploading LunarLander.apk onto device ‘emulator-5554′
    [2010-02-25 22:04:22 - LunarLander]Installing LunarLander.apk…
    [2010-02-25 22:04:38 - LunarLander]Success!
    [2010-02-25 22:04:38 - LunarLander]Starting activity com.example.android.lunarlander.LunarLander on device
    [2010-02-25 22:04:43 - LunarLander]ActivityManager: Starting: Intent { cmp=com.example.android.lunarlander/.LunarLander }

    Here’s how the app looks in the simulator (if you see the lock screen, slide the lock to the right using the mouse.
    az65

Feb
24
2010

Parsing XML in PHP Using SimpleXML

There are several method to parse XML data using PHP, one of them is using SimpleXML. SimpeXML requires PHP 5 or better but is one of the easiest way to parse XML in PHP, you can use object notation to refer to nodes. The SimpleXML API documentation is here: http://us3.php.net/manual/en/book.simplexml.php

Example 1

Here we show a very basic parsing, using xml text, passed using simplexml_load_string. Then we output the object returned by simplexml_load_string, shown below.  (To load from a file, there is also a simplexml_load_file function, an example of simplexml_load_file is linked at the end of this tutorial.)
<?php
$xmlData=simplexml_load_string("<root><node>This is a node</node></root>");
echo var_dump($xmlData);
?>
The output is below, as you can see, an object containing the XML tree has been created.

Example 2

Here we show how to access the element within the object returned by simplexml_load_string. It's just the basic object notations.
<?php
$xmlData=simplexml_load_string("<root><node>This is a node</node></root>");
echo $xmlData->node;
?>
The output is below:

Example 3

Accessing an attribute.
<?php
$xmlData=simplexml_load_string("<root><nodes><node1 exampleAttribute='sample attribute'>Node 1 Content</node1></nodes></root>");
echo "Node 1 content is ".$xmlData->nodes->node1;
echo "Node 1 attribute is ".$xmlData->nodes->node1->attributes()->exampleAttribute;
?>
The output is below:

Example 4

Iterating attributes.
<?php
$xmlData=simplexml_load_string("<root><node1 att1='ATT1' att2='ATT2'>Content</node1></root>");
foreach ($xmlData->node1->attributes() as $attributeName=>$attributeValue)
{
 echo "Node 1 has ".$attributeName . " with the value of " . $attributeValue;
}
?>
The output is below:

Example 5

Creating XML: you add child nodes and attributes to the xml object, using addChild and addAttributes.
<?php
$xmlData=simplexml_load_string("<xml></xml>");

// Adding chld nodes
$book1=$xmlData->addChild("book1");
$book2=$xmlData->addChild("book2");

// Adding attributes
$book1->addAttribute("title", "Book1Title");
$book2->addAttribute("title", "Book2Title");

$book1->addChild("author", "Jim");
$book2->addChild("author", "James");
?>
The code above creates an XML that looks like this:
<xml>
 <book1 title="Book1Title">
 <author>Jim</auhor>
 </book1>
 <book2 title="Book2Title">
 <author>James</author>
 </book2>
</xml>
Let's access some of the nodes and print them out using the code below:
<?php
echo $xmlData->book1->attributes()->title. "\n";
echo $xmlData->book2->attributes()->title. "\n";
echo $xmlData->book1->author. "\n";
echo $xmlData->book2->author. "\n";
?>
Here's the output:

Example 6

Creating XML This is using the same technique as example 5, but using using the SimpleXMLElement constructor. The constructor takes either a string or URL. Reference: http://us3.php.net/manual/en/simplexmlelement.construct.php.
<?php
$xmlData=new SimpleXMLElement("<xml></xml>");

// Adding child nodes
$book1=$xmlData->addChild("book1");
$book1->addAttribute("title", "Book1Title");
$book1->addChild("author", "Jim");

echo $book1->attributes()->title. "\n";
echo $book1->author. "\n";
?>
The output is as expected and shown below:
Head over to the next example showing how to use SimpleXML to parse RSS Feed:http://permadi.com/blog/2010/03/integratingreading-rss-feed-using-php/