简单!
全部博文(366)
分类: 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 文件数据,具体如下:]
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:
#includeThe Mini-XML library is included with your program using the -lmxml option:
gcc -o myprogram myprogram.c -lmxml ENTERIf 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 ENTEREvery 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 成员的成员。〕
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 |
|
|
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 mxmlNewElement, mxmlNewInteger, mxmlNewOpaque, mxmlNewReal, mxmlNewText mxmlNewTextf mxmlNewXML functions. Only elements can have child nodes, and the top node must be an element, usually the node created by mxmlNewXML().
[新节点使用 mxmlNewElement, mxmlNewInteger, mxmlNewOpaque, mxmlNewReal, mxmlNewText , 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)的指针。具体例子如下:]
the node tree for the file would look like the following in memory:
[在系统内存中以上 xml 文件节点树看起来如下所示:]
?xmlwhere "-" 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);