xml的用处越来越广泛了,解析xml得库也非常多,总的来说分为两种,一种是把xml当作一个“树”来进行解析,一种是基于事件类型的
glib就是使用事件类型解析xml。
有5种不同的事件类型
1)一个element的开始
2)一个element的结束
3)element得文本
4)一些stuff
5) 错误
There are five kinds of event which can happen:
- The start of an element
- The end of an element
- Some text (inside an element)
- Some other stuff (processing instructions, mainly, including comments and doctypes)
- An error
下面是一个简单的xml
simple.xml
:
<zoo>
<animal noise="roar">lion</animal>
<animal noise="sniffle">bunny</animal>
<animal noise="lol">cat</animal>
<keeper/>
</zoo>
被解析成以下的动作
- Start of “zoo”.
- Start of “animal”, with a “noise” attribute of “roar”.
- The text “lion”.
- End of “animal”.
- Start of “animal”, with a “noise” attribute of “sniffle”.
- The text “bunny”.
- End of “animal”.
- Start of “animal”, with a “noise” attribute of “lol”.
- The text “cat”.
- End of “animal”.
- Start of “keeper”.
- End of “keeper”.
- End of “zoo”.
程序运行结果
I am a lion and I go roar. Can you do it?
I am a bunny and I go sniffle. Can you do it?
I am a cat and I go lol. Can you do it?
#
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;
}
分享到:
相关推荐
亲测相当 好用,针对OLED的驱动移植例如SSD1306 ST7290等
用于windows下vc程序开发所调用glib库,有时候...glib库(SDK)文件,适应于windows 7 x64/x86开发调用,版本为v2.26.1,包含bin(主要的dll库)、lib(主要lib)、include(调用所需的头文件),希望有助于您的开发。
该库具有操作LCD12864的一些函数
本代码展示了如何利用glib库解析Key-value文件。本示例采用 Ubuntu18.04 + glib2.0 + Qt +c.
glib-2.56源代码。
GLib中文参考手册,学习GLib库的好帮手,提高自己的C语言开发水平。
glib 用vs2008编译好的,里面包含debug版本的lib文件
GLib包含了字符串操作、文件操作、数据校验、编解码、字符集转换、随机数生成器、命令行解析器、xml解析器、正则表达式、单链表、双链表、 数组、指针数组、双端队列、哈希表、平衡二叉树、N维树、泛型、主循环、多...
g l i b库是L i n u x平台下最常用的 C语言函数库,它具有很好的可移植性和实用性。 g l i b是 G t k +库和G n o m e的基础。g l i b可以在多个平台下使用,比如L...如果有兴趣,g l i b的源代码也是非常好的学习材料。
GLib Reference Manual for GLib 2.16.1 GLib 参考手册
glib-2.32.4.tar.xz linux
本文件主要包含GLib经过编译后的lib文件,在此基础上进行其他库的编译时需要使用
oled12864驱动,U8glib,12864液晶屏幕编程库。
压缩包包含GLib库运行时需要依赖的所有库文件
glib的Windows版本开发库,包含头文件及lib,运行时的动态库
glib安装详细教程
glib-2.0.dl
这个是glib2-devel,它和glib.2..不一样。 我是在编译heartbeat时,遇到缺少glib2-devel包的时候,下载的。 包含64位系统的和32位系统的