Parsing XML data in Tizen Native App

Introduction

Libxml2 is a commonly known XML parser and toolkit written in the C language and is freely available for integration into apps. Libxml2 library implements functions for reading, creating and manipulating XML data.

The strength of Libxml2 library is, it includes basic FTP and HTTP clients and there is no need to keep the XML file to be parsed in the local storage. In this tip document, the above mentioned library is used to build up a complete native application in Tizen which will provide the facility to use required data after parsing.

Step-1: Add headers for Libxml2 Library

#include <libxml/parser.h>
#include <libxml/tree.h>

Step-2: Initialization and Prerequisites

The following privilege should be added to use internet connection in the application.

Privilege

Description

http://tizen.org/privilege/internet

Allows the application to use the Internet connection.

In the example code, the tizen-manifest.xml is modified for having the required privileges as below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<manifest xmlns="http://tizen.org/ns/packages" api-version="2.4" pack-age="org.example.parsing" version="1.0.0">
    <profile name="mobile"/>
    <ui-application appid="org.example.parsing" exec="parsing" launch_mode="single" multiple="false" nodisplay="false" taskmanage="true" type="capp">
        <label>parsing</label>
        <icon>parsing.png</icon>
    </ui-application>
    <privileges>
        <privilege>http://tizen.org/privilege/internet</privilege>
    </privileges>
</manifest>

Step-3: Creating Data Structure for UI

Create the appdata data structure using the code below and use this to manage the UI elements of the application and store the content of the nodes of xml tree.

typedef struct appdata {
	Evas_Object *win;
	Evas_Object *conform;
	Evas_Object *label;
	bool value_begin;
	int value_type;
    	char buffer[1024];
} appdata_s;

Step-4: Initializing pointers and defining URL

Before starting parsing process the pointers should be initialized with NULL to avoid malfunctioning and the URL containing the XML file to be parsed should be defined as below. In this tutorial, a simple XML file is used for parsing as an example.

xmlDoc *doc = NULL;
xmlNode *root_element = NULL;

//give your URL below
char *loc = " http://www.xmlfiles.com/examples/simple.xml";

Step-5: Parse an XML file

The xmlReadFile function is used to parse an XML file from the network or filesystem. It returns the resulting document tree after a successful parse operation otherwise it will return NULL.

doc = xmlReadFile(loc, NULL, 0);

Step-6: Getting the root element

The xmlDocGetRootElement function returns the root element of the document.

root_element = xmlDocGetRootElement(doc);

Step-7: Getting the parsed data

The getParsedData function is used to go through the document and print all the element names in document order. The first parameter of this function is the root element of the tree.

getParsedData(root_element, ad);

At first, initialize an xmlNode type pointer variable with NULL to iterate through the tags of the xml tree using the code below:

xmlNode *cur_node = NULL;

After that, initialize an xmlChar type pointer variable with NULL to store the content of any Node. Use the code below:

xmlChar *value = NULL;

Now, use the cur_node variable and iterate through the nodes of xml tree including their child. Store the content of each node in buffer using the code below:

value = cur_node->content;
strcat(ad->buffer, (char*) value);

Step-8: Running the sample application

Now, the sample application is capable of showing the data of XML file required by user. Build and run the attached sample application. Here, the content of the <name>, <price> and <calories> tags of all the breakfast menu items from the xml tree are shown:

Figure 1 : example screenshot

References:

[1] http://www.developer.com/xml/article.php/3729826/Libxml2-Everything-You-Need-in-an-XML-Library.htm

文件附件: 
List
SDK Version Since: 
2.4 mobile/2.3.1 wearable