你可以考虑
迷你ML-Parser
,是一个简单而微小的C语言的XML解析器库。
Unlike other XML parser it is extremely easy to use. You need to call only one API to parse your XML data.
It is a validating parser. It validates the syntax of XML data and also extracts the content of XML data.
为了从XML数据中解析和提取内容,你需要向解析器提供一些信息。例如,XML元素名称、其子元素、属性、内容类型等。解析器使用
xs_element_t
结构来保存XML数据的所有这些属性
在你的例子中,你有两个XML元素 "user "和 "参数"。
user
是一个根元素,并且
参数
是一个子元素。属性 "name "和 "value "保存内容。为了提取这些内容,你需要指定内容类型和目标地址来存储这些内容。
例子
xs_element_t
为你的XML数据。
const xs_element_t user_root =
.Name.String = "user", //!< name of XML element
.Name.Length = 4, //!< Length of name
.Attribute_Quantity = 1, //!< Number of attributes of this element
.Attribute = attributes, //!< Address of structure containing attributes
.Child_Quantity = 1, //!< Number of child elements
.Child = ¶m_element, //!< Address of structure holding child elements
const xs_element_t param_element =
.Name.String = "param", //!< name of XML element
.Name.Length = 5, //!< Length of name
.Attribute_Quantity = 2, //!< Number of attributes of this element
.Attribute = attributes, //!< Address of structure containing attributes
//! Holds properties of attributes
const xs_attribute_t attributes[] =
[0].Name.String = "name", //!< Name of XML attribute
[0].Name.Length = 4, //!< Length of attribute name
[0].Target.Type = EN_STATIC, //!< Target address type is static.
[0].Target.Address = &name, //!< Target address offset from the parent target address
[0].Content.Type = EN_STRING, //!< Content type is string.
[1].Name.String = "value", //!< Name of XML attribute
[1].Name.Length = 4, //!< Length of attribute name
[1].Target.Type = EN_STATIC, //!< Target address type is static.
[1].Target.Address = &value, //!< Target address offset from the parent target address
[1].Content.Type = EN_STRING, //!< Content type is string.
注意:上述结构并不完整。为了使例子简单,我省略了一些结构成员。
要解析xml数据,请调用parse_xml()并传递地址为user_root(根元素)和XML数据。
parse_xml(&user_root, xml_data, NULL);