Mini-XML入门基础 Getting Started with Mini-XML c

Mini-XML入门基础 Getting Started with Mini-XML

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.
  • 支持在子节点中使用整型、浮点、字符数据(CDATA)、文本等数据类型
  • "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 schema 是用来定义 XML 文档的合法构建模块的,类似 DTD,但Mini-XML 不对基于XML Schema 的 数据做验证或其他处理,也不支持字符实体,当然必须支持XML规范要求的。

The Basics

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

#include <mxml.h>

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

Nodes

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
ValueTypeNode 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

mxml_node_s

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
child
First child node
last_child
Last child node
next
Next node under same parent
parent
Parent node
prev
Previous node under same parent
ref_count
Use count
type
Node type
user_data
User data
value
Node 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 <?xml version="1.0"?> node created by mxmlNewXML().

[新节点使用 mxmlNewElementmxmlNewIntegermxmlNewOpaquemxmlNewRealmxmlNewText ,  mxmlNewTextf  ,mxmlNewXML   函数创建。以上只有 元素(element)可以有子节点,同时 根节点必须是 元素,这个元素即是 <?xml version="1.0"?>节点,使用 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)的指针。具体例子如下:]

<?xml version="1.0"?>
<data>
<node>val1</node>
<node>val2</node>
<node>val3</node>
<group>
<node>val4</node>
<node>val5</node>
<node>val6</node>
</group>
<node>val7</node>
<node>val8</node>
</data>

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);

文章来源:http://hi.baidu.com/zhmsong/blog/item/9b35eb601402f6d48db10dde.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
感谢提供您的代码。根据您提供的代码,我注意到您在 `<actions>` 标签中缺少了一个闭合的 `</action>` 标签,导致 XML 结构错误。请将您的代码修改如下: ```xml <idea-plugin> <id>com.your.company.unique.plugin.id</id> <name>wkx_plugin</name> <version>1.0</version> <vendor email="[email protected]" url="http://www.hikvision.com">HikVision</vendor> <!-- description><![CDATA[ xml和excel互相转换插件.<br> www.wkx666.com</description --> <!-- change-notes><![CDATA[ Add change notes here.<br> most HTML tags may be used ]]> </change-notes--> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> <idea-version since-build="173.0"/> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products --> <depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> <actions> <action id="myAction" class="com.example.MyAction" text="My Action" description="My description"> <add-to-group group-id="MainMenu.Tools" anchor="last"/> <keyboard-shortcut first-keystroke="control alt A"/> </action> <!-- 添加一个闭合的 </action> 标签 --> </actions> </extensions> <actions> <!-- Add your actions here --> </actions> </idea-plugin> ``` 请确保 `<action>` 标签被正确地嵌套在 `<actions>` 标签内,并且每个 `<action>` 标签都有相应的闭合标签 `</action>`。 完成修改后,重新编译和运行插件,您应该能够在 "Tools" 菜单中看到您的新 action。如果问题仍然存在,请提供更多关于错误信息或其他相关代码的详细信息,以便我们能够更好地帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值