Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3963731
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2012-04-17 14:21:46

      This chapter describes how to write programs that use Mini-XML to access data in an XML file. Mini-XML provides the following functionality:

[主要内容是讲如何自己写C++程序使用 Mini-XML 访问或控制 XML 文件数据,具体如下:]

  • Functions for creating and managing XML documents in memory.
  • 在系统内存中创建和管理 XML 文件
  • Reading of UTF-8 and UTF-16 encoded XML files and strings.
  • 读取 UTF-8 和 UTF-16编码的 XML 文件和字符串
  • Writing of UTF-8 encoded XML files and strings.
  • 写入 UTF-8编码的XML文件和字符串
  • Support for arbitrary element names, attributes, and attribute values with no preset limits, just available memory.
  • 支持任意方式确定的元素名称、属性、属性值,可不按 W3C 标准?前提是只要内存可用
  • Support for integer, real, opaque ("cdata"), and text data types in "leaf" nodes.
  • 支持在子节点中使用整型、浮点、字符数据()、文本等数据类型
  • "Find", "index", and "walk" functions for easily accessing data in an XML document.
  • find、index、walk函数可以轻松地访问到xml文件中的数据。

Mini-XML doesn't do validation or other types of processing on the data based upon schema files or other sources of definition information, nor does it support character entities other than those required by the XML specification.

〔  是用来定义 XML 文档的合法构建模块的,类似 DTD,但Mini-XML 不对基于XML Schema 的 数据做验证或其他处理,也不支持,当然必须支持XML规范要求的。

Mini-XML provides a single header file which you include:

#include

The Mini-XML library is included with your program using the -lmxml option:

gcc -o myprogram myprogram.c -lmxml ENTER

If you have the pkg-config(1) software installed, you can use it to determine the proper compiler and linker options for your installation:

pkg-config --cflags mxml ENTER
pkg-config --libs mxml ENTER

Every piece of information in an XML file (elements, text, numbers) is stored in memory in "nodes". Nodes are defined by the mxml_node_t structure. The type member defines the node type (element, integer, opaque, real, or text) which determines which value you want to look at in the value union.〔XML文件的每块信息都以 “节点”的方式存储,在 Mini-XML 中 节点 由 mxml_node_t  结构体定义, mxml_node_t  的 type  成员定义了节点类型,节点类型决定了我们在 value 共同体中要查询的值的类型。下表是 mxml_node_t  的 mxml_value_t  成员的成员。

Table 2-1: Mini-XML Node Value Members
Value Type Node member
Custom void * node->value.custom.data
Element char * node->value.element.name
Integer int node->value.integer
Opaque (string) char * node->value.opaque
Real double node->value.real
Text char * node->value.text.string

An XML node.

struct mxml_node_s { 
struct mxml_node_s *child; 
struct mxml_node_s *last_child; 
struct mxml_node_s *next; 
struct mxml_node_s *parent; 
struct mxml_node_s *prev; 
int ref_count; 
mxml_type_t type; 
void *user_data; 
mxml_value_t value; 
};

 
  Members childFirst child nodelast_childLast child nodenextNext node under same parentparentParent nodeprevPrevious node under same parentref_countUse counttypeNode typeuser_dataUser datavalueNode value

Each node also has a user_data member which allows you to associate application-specific data with each node as needed.

每个节点结构体 mxml_node_t 还包括一个 user_data 成员,这允许使每个节点在需要时关联与具体应用有关的数据。

New nodes are created using the mxmlNewElementmxmlNewIntegermxmlNewOpaquemxmlNewRealmxmlNewText mxmlNewTextf mxmlNewXML functions. Only elements can have child nodes, and the top node must be an element, usually the  node created by mxmlNewXML().

[新节点使用 mxmlNewElementmxmlNewIntegermxmlNewOpaquemxmlNewRealmxmlNewText ,  mxmlNewTextf  ,mxmlNewXML   函数创建。以上只有 元素(element)可以有子节点,同时 根节点必须是 元素,这个元素即是 节点,使用 mxmlNewXML 创建。]

Nodes have pointers to the node above (parent), below ( child), left (prev), and right (next) of the current node. If you have an XML file like the following:

[节点结构体中有指向自身的上级节点(parent)、子节点(child)、相邻的上一个节点(prev)和相邻的下一个节点(next)的指针。具体例子如下:]



     val1
     val2
     val3
    
          val4
          val5
          val6
    

     val7
     val8

the node tree for the file would look like the following in memory:

[在系统内存中以上 xml 文件节点树看起来如下所示:]

   ?xml
     |
   data
     |
node - node - node - group - node - node
     |      |          |           |        |          |
   val1 val2      val3         |      val7      val8
                                      |
                                   node - node - node
                                      |         |        |
                                    val4    val5     val6

where "-" is a pointer to the next node and "|" is a pointer to the first child node.

以上“ - ” 代表指向相邻节点的指针,“ | ”代表指向第一个子节点的指针。

Once you are done with the XML data, use the mxmlDelete function to recursively free the memory that is used for a particular node or the entire tree:

一旦我们操作完 XML 数据,我们可以使用 mxmlDelete 函数释放内存,且迭代释放节点树上每个节点占用的内存。 ]

mxmlDelete(tree);
阅读(5643) | 评论(0) | 转发(1) |
0

上一篇:C/C++的XML解析库

下一篇:libiconv库裁减

给主人留下些什么吧!~~