Klipfolio. The KPI Dashboard - Evolved
The KPI Dashboard – Evolved
1-877-233-6149
Contact Us
TRY IT Web & Mobile
Sign In

Parsing data: XML, RSS, ATOM, RDF

Problem:

Your want to display XML data (including RSS, ATOM, RDF).

Solution:

Here are the general steps to creating a Klip that displays XML data:

Whenever you make changes in the Klip's source and save it, be sure to reload it.

In the example below, we will go through the above steps using the static sample XML data located at http://static.klipfolio.com/static/klips/samples/xml/data.xml.


XML data, whether in the RSS, ATOM or RDF format, is written in repeating blocks of opening and closing tags.

For example, in the following sample XML data, there are two <participant> blocks, each containing the same sets of information:

<telethon>
    <participant>
        <name>Cathy</name>
        <city>Tucson</city>
        <id>1024</id>
        <donation>100</donation>
    </participant>
    <participant>
        <name>Bob</name>
        <city>New Orleans</city>
        <id>1010</id>
        <donation>50</donation>
    </participant>
<telethon>

Let's say we want to write a Klip that displays all the available information about each participant, with his or her name, city, id and donation values displayed in a row, or as an "item", in the Klip. Using the above sample data, we should see 2 items when we are finished with the Klip.

1. Specify the <contentsource>

We suggest that you start out with a blank Klip rather than a template if you are not familiar with Klip development. To add one, click the New button in the toolbar, then select Blank Klip.

First, let's update the <contentsource> with the URL of the sample data. Look for <contentsource> in the Klip and update it like this:

<contentsource>
    http://static.klipfolio.com/static/klips/samples/xml/data.xml
</contentsource>

This is where the sample XML data is located.

Save and reload the Klip to allow the change to be reflected.
http://www.klipfolio.com/static/klips/samples/xml/xml1.png

The important thing here is that the Klip displays no data -- its title and icon may be different in yours depending on its <title> and <icon> settings.

No data is being displayed at this point because the Klip doesn't know what part of the content source to parse. For this, we must write a <style> block that is specific to this data source.

2. Write the <style> block

The syntax for writing the <style> block is as follows:

selector {
    property: value;
}

The selector is the XML tag for which you want to write the styles. The first thing we need to do is to specify what constitutes an item, which in the sample data is <participant> to </participant>:

<style>
    participant {
        type: item;
    }
</style>

Regardless of the data source type, you always have to specify type: item for one selector.To display the <name> values in the 1st column, use the itemcol property with 1:

<style>
    participant {
        type: item;
    }
    
    name {
        itemcol: 1;    
    }
</style>

When you save the updates and reload the Klip, it should display 2 items:
http://www.klipfolio.com/static/klips/samples/xml/xml2.png

Similarly, add the column settings for the <city>, <id> and <donation> values, then save and reload the Klip:

city {
    itemcol: 2;
}
id {
    itemcol: 3;
}
donation {
    itemcol: 4;
}

The Klip should look like this now:
http://www.klipfolio.com/static/klips/samples/xml/xml3.png

As far as displaying the data goes, this is it. However, you will want to make some customizations so that the Klip presents your data in a more coherent manner.

3. Specify additional settings

When you don't specify the type for a column, its data is treated as text (i.e. type: text;) by default. This is fine for the names, cities, and IDs, but for the donation column, we'll want the values treated as numbers, then perhaps displayed with a dollar sign and two decimal places. Update the donation style as follows:

donation {
    type: number;
    itemcol: 4;
    format: "currency($),decimal(2)";
}

It doesn't matter in what order the settings appear within the brackets. When you save the change and reload the Klip, it should look like this:
http://www.klipfolio.com/static/klips/samples/xml/xml4.png

Download the finished sample XML Klip.

For additional customization options, refer to the how-to article Top 5 Klip Development Best Practices.

For the complete list of supported style properties, refer to the Klip Developer Guide.

Leave a Comment

To leave a comment, you must sign in or register (it's free).

   
Comment Type:
Title:
Comment: