glib 解析xml
来源:互联网 发布:csol企鹅优化 编辑:程序博客网 时间:2024/05/16 11:56
有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
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;
}
- glib 解析xml
- glib 学习笔记,解析xml文件
- Glib解析命令行参数
- glib命令行解析简单示例
- Using GLib's GKeyFile Parser (使用GLib解析配置文件)
- Using GLib's GKeyFile Parser (使用Glib解析配置文件)
- Using GLib's GKeyFile Parser (使用GLib解析配置文件)
- Using GLib's GKeyFile Parser (使用Glib解析配置文件)
- glib
- GLib
- Glib
- glib
- glib
- glib命令行解析库简单使用
- 转 glib命令行解析库简单使用
- glib命令行解析库简单使用--GOptionEntry 命令行参数
- XML解析
- xml解析
- 用jquery实现的模拟QQ邮箱里的收件人选取及其他效果
- const关键字详解
- 用Apache POI来实现对Excel的读写
- Shell IF判断语句
- Oracle 重建控制文件 对 只读表空间 和 临时表空间 影响 测试
- glib 解析xml
- 基于funambol ds 的云同步服务研究(五)-结合Sync Client的测试使用
- VB语言基础
- Android有四种方式(一)
- 开门七件事
- LoadRunner中文出现乱码的解决办法
- HTTP header 详解
- Hacking python系列教程
- 春晚宫女唐奕霖被爆背景深厚引热议