1mxml(3) Michael Sweet mxml(3)
2
3
4
6 mxml - mini-xml library
7
9 #include <mxml.h>
10
12 -lmxml
13
15 Mini-XML is a small XML parsing library that you can use to read XML
16 and XML-like data files in your application without requiring large
17 non-standard libraries. Mini-XML only requires an ANSI C compatible
18 compiler (GCC works, as do most vendors' ANSI C compilers) and a "make"
19 program.
20
21 Mini-XML provides the following functionality:
22
23 · Reading and writing of UTF-8 encoded XML files.
24
25 · Reading and writing of UTF-8 encoded XML strings.
26
27 · Data is stored in a linked-list tree structure, preserving the XML
28 data hierarchy.
29
30 · Supports arbitrary element names, attributes, and attribute values
31 with no preset limits, just available memory.
32
33 · Supports integer, real, opaque ("cdata"), and text data types in
34 "leaf" nodes.
35
36 · Functions for creating and managing trees of data.
37
38 · "Find" and "walk" functions for easily locating and navigating
39 trees of data.
40
41 Mini-XML doesn't do validation or other types of processing on the data
42 based upon schema files or other sources of definition information, nor
43 does it support character entities other than those required by the XML
44 specification. Also, since Mini-XML does not support the UTF-16 encod‐
45 ing, it is technically not a conforming XML consumer/client.
46
48 Mini-XML provides a single header file which you include:
49
50 #include <mxml.h>
51
52 Nodes are defined by the "mxml_node_t" structure; the "type" member
53 defines the node type (element, integer, opaque, real, or text) which
54 determines which value you want to look at in the "value" union. New
55 nodes can be created using the "mxmlNewElement()", "mxmlNewInteger()",
56 "mxmlNewOpaque()", "mxmlNewReal()", and "mxmlNewText()" functions.
57 Only elements can have child nodes, and the top node must be an ele‐
58 ment, usually "?xml".
59
60 You load an XML file using the "mxmlLoadFile()" function:
61
62 FILE *fp;
63 mxml_node_t *tree;
64
65 fp = fopen("filename.xml", "r");
66 tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
67 fclose(fp);
68
69 Similarly, you save an XML file using the "mxmlSaveFile()" function:
70
71 FILE *fp;
72 mxml_node_t *tree;
73
74 fp = fopen("filename.xml", "w");
75 mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
76 fclose(fp);
77
78 The "mxmlLoadString()", "mxmlSaveAllocString()", and "mxmlSaveString()"
79 functions load XML node trees from and save XML node trees to strings:
80
81 char buffer[8192];
82 char *ptr;
83 mxml_node_t *tree;
84
85 ...
86 tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
87
88 ...
89 mxmlSaveString(tree, buffer, sizeof(buffer),
90 MXML_NO_CALLBACK);
91
92 ...
93 ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
94
95 You can find a named element/node using the "mxmlFindElement()" func‐
96 tion:
97
98 mxml_node_t *node = mxmlFindElement(tree, tree, "name",
99 "attr", "value",
100 MXML_DESCEND);
101
102 The "name", "attr", and "value" arguments can be passed as NULL to act
103 as wildcards, e.g.:
104
105 /* Find the first "a" element */
106 node = mxmlFindElement(tree, tree, "a", NULL, NULL,
107 MXML_DESCEND);
108
109 /* Find the first "a" element with "href" attribute */
110 node = mxmlFindElement(tree, tree, "a", "href", NULL,
111 MXML_DESCEND);
112
113 /* Find the first "a" element with "href" to a URL */
114 node = mxmlFindElement(tree, tree, "a", "href",
115 "http://www.easysw.com/~mike/mxml/",
116 MXML_DESCEND);
117
118 /* Find the first element with a "src" attribute*/
119 node = mxmlFindElement(tree, tree, NULL, "src", NULL,
120 MXML_DESCEND);
121
122 /* Find the first element with a "src" = "foo.jpg" */
123 node = mxmlFindElement(tree, tree, NULL, "src",
124 "foo.jpg", MXML_DESCEND);
125
126 You can also iterate with the same function:
127
128 mxml_node_t *node;
129
130 for (node = mxmlFindElement(tree, tree, "name", NULL,
131 NULL, MXML_DESCEND);
132 node != NULL;
133 node = mxmlFindElement(node, tree, "name", NULL,
134 NULL, MXML_DESCEND))
135 {
136 ... do something ...
137 }
138
139 Finally, once you are done with the XML data, use the "mxmlDelete()"
140 function to recursively free the memory that is used for a particular
141 node or the entire tree:
142
143 mxmlDelete(tree);
144
146 mxmldoc(1), Mini-XML Programmers Manual,
147 http://www.easysw.com/~mike/mxml/
148
150 Copyright 2003-2005 by Michael Sweet.
151
152
153
15425 February 2005 mini-XML mxml(3)