1
/* $Id: xml.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* @brief PJLIB XML Parser/Helper.
34
* @defgroup PJ_TINY_XML Mini/Tiny XML Parser/Helper
35
* @ingroup PJ_FILE_FMT
39
/** Typedef for XML attribute. */
40
typedef struct pj_xml_attr pj_xml_attr;
42
/** Typedef for XML nodes. */
43
typedef struct pj_xml_node pj_xml_node;
45
/** This structure declares XML attribute. */
48
PJ_DECL_LIST_MEMBER(pj_xml_attr); /**< Standard list elements. */
49
pj_str_t name; /**< Attribute name. */
50
pj_str_t value; /**< Attribute value. */
53
/** This structure describes XML node head inside XML node structure.
55
typedef struct pj_xml_node_head
57
PJ_DECL_LIST_MEMBER(pj_xml_node); /**< Standard list elements. */
60
/** This structure describes XML node. */
63
PJ_DECL_LIST_MEMBER(pj_xml_node); /**< List @a prev and @a next member */
64
pj_str_t name; /**< Node name. */
65
pj_xml_attr attr_head; /**< Attribute list. */
66
pj_xml_node_head node_head; /**< Node list. */
67
pj_str_t content; /**< Node content. */
71
* Parse XML message into XML document with a single root node. The parser
72
* is capable of parsing XML processing instruction construct ("<?") and
73
* XML comments ("<!--"), however such constructs will be ignored and will not
74
* be included in the resulted XML node tree.
76
* @param pool Pool to allocate memory from.
77
* @param msg The XML message to parse.
78
* @param len The length of the message.
80
* @return XML root node, or NULL if the XML document can not be parsed.
82
PJ_DECL(pj_xml_node*) pj_xml_parse( pj_pool_t *pool, char *msg, pj_size_t len);
86
* Print XML into XML message. Note that the function WILL NOT NULL terminate
89
* @param node The XML node to print.
90
* @param buf Buffer to hold the output message.
91
* @param len The length of the buffer.
92
* @param prolog If set to nonzero, will print XML prolog ("<?xml..")
94
* @return The size of the printed message, or -1 if there is not
95
* sufficient space in the buffer to print the message.
97
PJ_DECL(int) pj_xml_print( const pj_xml_node *node, char *buf, pj_size_t len,
101
* Clone XML node and all subnodes.
103
* @param pool Pool to allocate memory for new nodes.
104
* @param rhs The node to clone.
106
* @return Cloned XML node, or NULL on fail.
108
PJ_DECL(pj_xml_node*) pj_xml_clone( pj_pool_t *pool, const pj_xml_node *rhs);
112
* Create an empty node.
115
* @param name Node name.
117
* @return The new node.
119
PJ_DECL(pj_xml_node*) pj_xml_node_new(pj_pool_t *pool, const pj_str_t *name);
123
* Create new XML attribute.
126
* @param name Attribute name.
127
* @param value Attribute value.
129
* @return The new XML attribute.
131
PJ_DECL(pj_xml_attr*) pj_xml_attr_new(pj_pool_t *pool, const pj_str_t *name,
132
const pj_str_t *value);
135
* Add node to another node.
137
* @param parent Parent node.
138
* @param node Node to be added to parent.
140
PJ_DECL(void) pj_xml_add_node( pj_xml_node *parent, pj_xml_node *node );
144
* Add attribute to a node.
147
* @param attr Attribute to add to node.
149
PJ_DECL(void) pj_xml_add_attr( pj_xml_node *node, pj_xml_attr *attr );
152
* Find first direct child node with the specified name.
154
* @param parent Parent node.
155
* @param name Node name to find.
157
* @return XML node found or NULL.
159
PJ_DECL(pj_xml_node*) pj_xml_find_node(const pj_xml_node *parent,
160
const pj_str_t *name);
163
* Find next direct child node with the specified name.
165
* @param parent Parent node.
166
* @param node node->next is the starting point.
167
* @param name Node name to find.
169
* @return XML node found or NULL.
171
PJ_DECL(pj_xml_node*) pj_xml_find_next_node(const pj_xml_node *parent,
172
const pj_xml_node *node,
173
const pj_str_t *name);
176
* Recursively find the first node with the specified name in the child nodes
177
* and their children.
179
* @param parent Parent node.
180
* @param name Node name to find.
182
* @return XML node found or NULL.
184
PJ_DECL(pj_xml_node*) pj_xml_find_node_rec(const pj_xml_node *parent,
185
const pj_str_t *name);
189
* Find first attribute within a node with the specified name and optional
192
* @param node XML Node.
193
* @param name Attribute name to find.
194
* @param value Optional value to match.
196
* @return XML attribute found, or NULL.
198
PJ_DECL(pj_xml_attr*) pj_xml_find_attr(const pj_xml_node *node,
199
const pj_str_t *name,
200
const pj_str_t *value);
204
* Find a direct child node with the specified name and match the function.
206
* @param parent Parent node.
207
* @param name Optional name. If this is NULL, the name will not be
209
* @param data Data to be passed to matching function.
210
* @param match Optional matching function.
212
* @return The first matched node, or NULL.
214
PJ_DECL(pj_xml_node*) pj_xml_find( const pj_xml_node *parent,
215
const pj_str_t *name,
217
pj_bool_t (*match)(const pj_xml_node *,
222
* Recursively find a child node with the specified name and match the
225
* @param parent Parent node.
226
* @param name Optional name. If this is NULL, the name will not be
228
* @param data Data to be passed to matching function.
229
* @param match Optional matching function.
231
* @return The first matched node, or NULL.
233
PJ_DECL(pj_xml_node*) pj_xml_find_rec(const pj_xml_node *parent,
234
const pj_str_t *name,
236
pj_bool_t (*match)(const pj_xml_node*,
246
#endif /* __PJ_XML_H__ */