博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
glib 解析xml
阅读量:4106 次
发布时间:2019-05-25

本文共 2471 字,大约阅读时间需要 8 分钟。

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 :

lion
bunny
cat

被解析成以下的动作

  • 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;
}

转载地址:http://ucjsi.baihongyu.com/

你可能感兴趣的文章
问题解决方法收集归档
查看>>
linux系统重要源码、工具归档
查看>>
linux知识包
查看>>
linux内核设计与实现[第2章 摘抄]
查看>>
linux内核设计与实现[第3章 摘抄]
查看>>
数据结构之线性表
查看>>
lua学习笔记
查看>>
list0307
查看>>
Markdown编辑器写博客
查看>>
嵌入式linux软件开发学习笔记--uboot环境变量命令行的使用
查看>>
嵌入式linux软件开发学习笔记--uboot介绍
查看>>
C/C++面试题总结(2)
查看>>
QEMU搭建arm Linux开发环境
查看>>
C语言嵌入式--数据存储与指针笔记6
查看>>
IMX6相关资料归档
查看>>
wireshark笔记
查看>>
arm linux kernel编译问题总结
查看>>
qemu模拟开发板问题记录
查看>>
virtualBOX + ubuntu 与 windows共享文件夹
查看>>
linux qt编译安装问题解决记录
查看>>