-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml_parser.h
117 lines (95 loc) · 3.33 KB
/
xml_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef __XML_PARSER_H__
#define __XML_PARSER_H__
#include <stdbool.h>
typedef struct XMLAttr {
char *key;
char *value;
struct XMLNode *node; //Node which the attribute belongs
}XMLAttr;
typedef struct XMLAttrList {
size_t count;
size_t capacity;
XMLAttr *attrs;
}XMLAttrList;
typedef struct XMLNodeList {
size_t count;
size_t capacity;
struct XMLNode **nodes;
}XMLNodeList;
typedef enum NodeType{
NT_NODE,
NT_TEXT,
NT_COMMENT,
NT_CDATA,
NT_PI, /* processing instruction */
NT_DOCTYPE
}NodeType;
typedef struct XMLNode {
NodeType type;
char *name;
char *text;
struct XMLNode *parent;
XMLAttrList attrList;
XMLNodeList children;
size_t index; /* index in parent's children. The index start at 0 */
}XMLNode;
typedef struct XMLDocument {
char *contents;
XMLNodeList others; /* other nodes before root */
XMLNode *root;
//char *version;
//char *encoding;
}XMLDocument;
/* Filter node callback */
typedef bool (*Predicate)(XMLNode *node, int idx, void *user_data);
/* Select node callback */
typedef XMLNodeList *(*Selector)(XMLNode *node, int idx, void *user_data);
/* XML AttributeList */
void XMLAttrListInit(XMLAttrList *list);
void XMLAttrListAdd(XMLAttrList *list, XMLAttr *attr);
XMLAttr* XMLAttrListGet(XMLAttrList *list, int index);
size_t XMLAttrListCount(XMLAttrList *list);
void XMLAttrListFree(XMLAttrList *list);
/* XML NodeList */
void XMLNodeListInit(XMLNodeList *list);
void XMLNodeListAdd(XMLNodeList *list, XMLNode *node);
void XMLNodeListAddList(XMLNodeList *list, XMLNodeList *srcList);
XMLNode *XMLNodeListGet(XMLNodeList *list, int index);
XMLNode *XMLNodeListRemove(XMLNodeList *list);
size_t XMLNodeListCount(XMLNodeList *list);
void XMLNodeListFree(XMLNodeList *list);
/* XML Node */
/* Get children node at index */
XMLNode *XMLNodeChildrenGet(XMLNode *node, int index);
/* Get children count */
size_t XMLNodeChildrenCount(XMLNode *node);
/* Select node which satisfy node_path.
* Note: 'node_path' doesn't support attribute, only Node.
* */
XMLNode *XMLSelectNode(XMLNode *node, const char *node_path);
#define XML_ROOT(doc) (doc)->root
XMLNode *XMLRootNode(XMLDocument *doc);
/* Find first node of `node` with name `node_name` */
XMLNode *XMLFindFirstNode(const XMLNode *node, const char *node_name);
/* Find all child node of `node` with `node_name`.
* Note: you must free the returned XMLNodeList if the result is not NULL;
* */
XMLNodeList *XMLFindNode(const XMLNode *node, const char *node_name);
/* Fina all child node of `node` which satify `Predicate`.
* Note: you must free the returned XMLNodeList if the result is not NULL;
* */
XMLNodeList *XMLFindNodeWhere(const XMLNode *node, Predicate predicateFn, void *user_data);
/* Find all child node of `node` which satify `Selector`.
* Note: you must free the returned XMLNodeList if the result is not NULL;
* */
XMLNodeList *XMLFindNodeSelector(const XMLNode *node, Selector selectFn, void *user_data);
/* decode xml node text */
char *XMLDecodeText(const XMLNode *node);
/* Get the next sibling node or NULL if `node` is the last child */
XMLNode *XMLNodeNextSibling(XMLNode *node);
/* XML Document */
bool XMLDocumentParseFile(XMLDocument *doc, const char *path);
bool XMLDocumentParseStr(XMLDocument *doc, const char *xmlStr);
void XMLPrettyPrint(XMLDocument *doc, FILE *fp, int ident_len);
void XMLDocumentFree(XMLDocument *doc);
#endif