glib 学习笔记,解析xml文件
2009-02-04 10:27
531 查看
转载请注明出处,或联系
fanyuanmail@126.com
xml的用处越来越广泛了,解析xml得库也非常多,总的来说分为两种,一种是把xml当作一个“树”来进行解析,一种是基于事件类型的
glib就是使用事件类型解析xml。
#include stdio.h>
gchar *current_animal_noise = NULL;
static void start(GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
const gchar **name_cursor = attribute_names;
const gchar **value_cursor = attribute_values;
while (*name_cursor) {
if (strcmp (*name_cursor, "noise") == 0)
current_animal_noise = g_strdup (*value_cursor);
name_cursor++;
value_cursor++;
}
}
static void end(GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
if (current_animal_noise)
{
g_free (current_animal_noise);
current_animal_noise = NULL;
}
}
static void text(GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
if (current_animal_noise)
printf("I am a %*s and I go %s. Can you do it?/n",
text_len, text, current_animal_noise);
printf("test text/n");
}
GMarkupParser parser = {
.start_element = start,
.end_element = end,
.text = text,
.passthrough = NULL,
.error = NULL
};
int main()
{
char *buf;
gsize length;
GMarkupParseContext *context;
g_file_get_contents("test.xml", &buf, &length,NULL);
g_printf("%s/n",buf);
context = g_markup_parse_context_new(&parser, 0, NULL, NULL);
if (g_markup_parse_context_parse(context, buf, length, NULL) == FALSE)
{
printf("Couldn't load xml/n");
g_markup_parse_context_free(context);
return 0;
}
fanyuanmail@126.com
xml的用处越来越广泛了,解析xml得库也非常多,总的来说分为两种,一种是把xml当作一个“树”来进行解析,一种是基于事件类型的
glib就是使用事件类型解析xml。
#include stdio.h>
gchar *current_animal_noise = NULL;
static void start(GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
const gchar **name_cursor = attribute_names;
const gchar **value_cursor = attribute_values;
while (*name_cursor) {
if (strcmp (*name_cursor, "noise") == 0)
current_animal_noise = g_strdup (*value_cursor);
name_cursor++;
value_cursor++;
}
}
static void end(GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
if (current_animal_noise)
{
g_free (current_animal_noise);
current_animal_noise = NULL;
}
}
static void text(GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
if (current_animal_noise)
printf("I am a %*s and I go %s. Can you do it?/n",
text_len, text, current_animal_noise);
printf("test text/n");
}
GMarkupParser parser = {
.start_element = start,
.end_element = end,
.text = text,
.passthrough = NULL,
.error = NULL
};
int main()
{
char *buf;
gsize length;
GMarkupParseContext *context;
g_file_get_contents("test.xml", &buf, &length,NULL);
g_printf("%s/n",buf);
context = g_markup_parse_context_new(&parser, 0, NULL, NULL);
if (g_markup_parse_context_parse(context, buf, length, NULL) == FALSE)
{
printf("Couldn't load xml/n");
g_markup_parse_context_free(context);
return 0;
}
相关文章推荐
- Symbian学习笔记(15) - 解析XML文件(上)
- 学习笔记-Java中的xml文件读取之DOM4J解析
- Java之JDOM方式解析XML文件(学习笔记)
- 慕课学习笔记之DOM,SAX,JDOM,DOM4J解析XML文件
- Mars Android视频学习笔记——01_20_XML文件解析
- xml学习笔记②PHP DOM--对xml文件进行解析操作
- 个人学习笔记之-DOM解析XML文件
- android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化
- Mars Android视频学习笔记——01_20_XML文件解析
- Symbian学习笔记(16) - 解析XML文件(下)
- Android 个人学习笔记之---SAX解析XML文件(有一个坑爹的问题)
- Symbian学习笔记(15) - 解析XML文件(上)
- 学习笔记-Java中的xml文件读取之JDOM解析
- 【学习笔记】JAVA之使用dom4j对xml文件解析
- Java之DOM4J方式解析XML文件(学习笔记)
- 【coder-pig教程学习笔记4】xml文件解析
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- Symbian学习笔记——解析XML文件(上)
- 学习笔记之利用ajax请求xml文件,解析其中内容
- Symbian学习笔记——解析XML文件(下)