mxml.3 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. .TH mxml 3 "Mini-XML API" "2022-07-25" "Mini-XML API"
  2. .SH NAME
  3. mxml \- Mini-XML API
  4. .SH INCLUDE FILE
  5. #include <mxml.h>
  6. .SH LIBRARY
  7. \-lmxml
  8. .SH DESCRIPTION
  9. Mini-XML is a small XML parsing library that you can use to
  10. read XML and XML-like data files in your application without
  11. requiring large non-standard libraries. Mini-XML only
  12. requires an ANSI C compatible compiler (GCC works, as do
  13. most vendors' ANSI C compilers) and a "make" program.
  14. .PP
  15. Mini-XML provides the following functionality:
  16. .IP \(bu 4
  17. Reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and strings.
  18. .IP \(bu 4
  19. Data is stored in a linked-list tree structure,
  20. preserving the XML data hierarchy.
  21. .IP \(bu 4
  22. Supports arbitrary element names, attributes, and attribute
  23. values with no preset limits, just available memory.
  24. .IP \(bu 4
  25. Supports integer, real, opaque ("CDATA"), and text data types in
  26. "leaf" nodes.
  27. .IP \(bu 4
  28. Functions for creating, indexing, and managing trees of data.
  29. .IP \(bu 4
  30. "Find" and "walk" functions for easily locating and navigating
  31. trees of data.
  32. .PP
  33. Mini-XML doesn't do validation or other types of processing
  34. on the data based upon schema files or other sources of
  35. definition information, nor does it support character
  36. entities other than those required by the XML
  37. specification.
  38. .SH USING MINI-XML
  39. Mini-XML provides a single header file which you include:
  40. .nf
  41. #include <mxml.h>
  42. .fi
  43. .PP
  44. Nodes are defined by the "mxml_node_t" structure; the "type"
  45. member defines the node type (element, integer, opaque,
  46. real, or text) which determines which value you want to look
  47. at in the "value" union. New nodes can be created using the
  48. "mxmlNewElement()", "mxmlNewInteger()", "mxmlNewOpaque()",
  49. "mxmlNewReal()", and "mxmlNewText()" functions. Only
  50. elements can have child nodes, and the top node must be an
  51. element, usually "?xml".
  52. .PP
  53. You load an XML file using the "mxmlLoadFile()" function:
  54. .nf
  55. FILE *fp;
  56. mxml_node_t *tree;
  57. fp = fopen("filename.xml", "r");
  58. tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
  59. fclose(fp);
  60. .fi
  61. .PP
  62. Similarly, you save an XML file using the "mxmlSaveFile()"
  63. function:
  64. .nf
  65. FILE *fp;
  66. mxml_node_t *tree;
  67. fp = fopen("filename.xml", "w");
  68. mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
  69. fclose(fp);
  70. .fi
  71. .PP
  72. The "mxmlLoadString()", "mxmlSaveAllocString()", and
  73. "mxmlSaveString()" functions load XML node trees from and save
  74. XML node trees to strings:
  75. .nf
  76. char buffer[8192];
  77. char *ptr;
  78. mxml_node_t *tree;
  79. ...
  80. tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
  81. ...
  82. mxmlSaveString(tree, buffer, sizeof(buffer),
  83. MXML_NO_CALLBACK);
  84. ...
  85. ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
  86. .fi
  87. .PP
  88. You can find a named element/node using the "mxmlFindElement()"
  89. function:
  90. .nf
  91. mxml_node_t *node = mxmlFindElement(tree, tree, "name",
  92. "attr", "value",
  93. MXML_DESCEND);
  94. .fi
  95. .PP
  96. The "name", "attr", and "value" arguments can be passed as
  97. NULL to act as wildcards, e.g.:
  98. .nf
  99. /* Find the first "a" element */
  100. node = mxmlFindElement(tree, tree, "a", NULL, NULL,
  101. MXML_DESCEND);
  102. /* Find the first "a" element with "href" attribute */
  103. node = mxmlFindElement(tree, tree, "a", "href", NULL,
  104. MXML_DESCEND);
  105. /* Find the first "a" element with "href" to a URL */
  106. node = mxmlFindElement(tree, tree, "a", "href",
  107. "http://www.easysw.com/~mike/mxml/",
  108. MXML_DESCEND);
  109. /* Find the first element with a "src" attribute*/
  110. node = mxmlFindElement(tree, tree, NULL, "src", NULL,
  111. MXML_DESCEND);
  112. /* Find the first element with a "src" = "foo.jpg" */
  113. node = mxmlFindElement(tree, tree, NULL, "src",
  114. "foo.jpg", MXML_DESCEND);
  115. .fi
  116. .PP
  117. You can also iterate with the same function:
  118. .nf
  119. mxml_node_t *node;
  120. for (node = mxmlFindElement(tree, tree, "name", NULL,
  121. NULL, MXML_DESCEND);
  122. node != NULL;
  123. node = mxmlFindElement(node, tree, "name", NULL,
  124. NULL, MXML_DESCEND))
  125. {
  126. ... do something ...
  127. }
  128. .fi
  129. .PP
  130. To find the value of a specific node in the tree, use the "mxmlFindPath()"
  131. function:
  132. .nf
  133. mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar");
  134. .fi
  135. .PP
  136. The "mxmlGetInteger()", "mxmlGetOpaque()", "mxmlGetReal()", and "mxmlGetText()"
  137. functions retrieve the value from a node:
  138. .nf
  139. mxml_node_t *node;
  140. int intvalue = mxmlGetInteger(node);
  141. const char *opaquevalue = mxmlGetOpaque(node);
  142. double realvalue = mxmlGetReal(node);
  143. int whitespacevalue;
  144. const char *textvalue = mxmlGetText(node, &whitespacevalue);
  145. .fi
  146. .PP
  147. Finally, once you are done with the XML data, use the
  148. "mxmlDelete()" function to recursively free the memory that
  149. is used for a particular node or the entire tree:
  150. .nf
  151. mxmlDelete(tree);
  152. .fi
  153. .SH ENUMERATIONS
  154. .SS mxml_sax_event_e
  155. SAX event type.
  156. .TP 5
  157. MXML_SAX_CDATA
  158. .br
  159. CDATA node
  160. .TP 5
  161. MXML_SAX_COMMENT
  162. .br
  163. Comment node
  164. .TP 5
  165. MXML_SAX_DATA
  166. .br
  167. Data node
  168. .TP 5
  169. MXML_SAX_DIRECTIVE
  170. .br
  171. Processing directive node
  172. .TP 5
  173. MXML_SAX_ELEMENT_CLOSE
  174. .br
  175. Element closed
  176. .TP 5
  177. MXML_SAX_ELEMENT_OPEN
  178. .br
  179. Element opened
  180. .SS mxml_type_e
  181. The XML node type.
  182. .TP 5
  183. MXML_CUSTOM
  184. .br
  185. Custom data
  186. .TP 5
  187. MXML_ELEMENT
  188. .br
  189. XML element with attributes
  190. .TP 5
  191. MXML_IGNORE
  192. .br
  193. Ignore/throw away node
  194. .TP 5
  195. MXML_INTEGER
  196. .br
  197. Integer value
  198. .TP 5
  199. MXML_OPAQUE
  200. .br
  201. Opaque string
  202. .TP 5
  203. MXML_REAL
  204. .br
  205. Real value
  206. .TP 5
  207. MXML_TEXT
  208. .br
  209. Text fragment
  210. .SH FUNCTIONS
  211. .SS mxmlAdd
  212. Add a node to a tree.
  213. .PP
  214. .nf
  215. void mxmlAdd (
  216. mxml_node_t *parent,
  217. int where,
  218. mxml_node_t *child,
  219. mxml_node_t *node
  220. );
  221. .fi
  222. .PP
  223. Adds the specified node to the parent. If the child argument is not
  224. \fBNULL\fR, puts the new node before or after the specified child depending
  225. on the value of the where argument. If the child argument is \fBNULL\fR,
  226. puts the new node at the beginning of the child list (\fBMXML_ADD_BEFORE\fR)
  227. or at the end of the child list (\fBMXML_ADD_AFTER\fR). The constant
  228. \fBMXML_ADD_TO_PARENT\fR can be used to specify a \fBNULL\fR child pointer.
  229. .SS mxmlDelete
  230. Delete a node and all of its children.
  231. .PP
  232. .nf
  233. void mxmlDelete (
  234. mxml_node_t *node
  235. );
  236. .fi
  237. .PP
  238. If the specified node has a parent, this function first removes the
  239. node from its parent using the \fImxmlRemove\fR function.
  240. .SS mxmlElementDeleteAttr
  241. Delete an attribute.
  242. .PP
  243. .nf
  244. void mxmlElementDeleteAttr (
  245. mxml_node_t *node,
  246. const char *name
  247. );
  248. .fi
  249. .PP
  250. .SS mxmlElementGetAttr
  251. Get an attribute.
  252. .PP
  253. .nf
  254. const char * mxmlElementGetAttr (
  255. mxml_node_t *node,
  256. const char *name
  257. );
  258. .fi
  259. .PP
  260. This function returns \fBNULL\fR if the node is not an element or the
  261. named attribute does not exist.
  262. .SS mxmlElementGetAttrByIndex
  263. Get an element attribute by index.
  264. .PP
  265. .nf
  266. const char * mxmlElementGetAttrByIndex (
  267. mxml_node_t *node,
  268. int idx,
  269. const char **name
  270. );
  271. .fi
  272. .PP
  273. The index ("idx") is 0-based. \fBNULL\fR is returned if the specified index
  274. is out of range.
  275. .SS mxmlElementGetAttrCount
  276. Get the number of element attributes.
  277. .PP
  278. .nf
  279. int mxmlElementGetAttrCount (
  280. mxml_node_t *node
  281. );
  282. .fi
  283. .PP
  284. .SS mxmlElementSetAttr
  285. Set an attribute.
  286. .PP
  287. .nf
  288. void mxmlElementSetAttr (
  289. mxml_node_t *node,
  290. const char *name,
  291. const char *value
  292. );
  293. .fi
  294. .PP
  295. If the named attribute already exists, the value of the attribute
  296. is replaced by the new string value. The string value is copied
  297. into the element node. This function does nothing if the node is
  298. not an element.
  299. .SS mxmlElementSetAttrf
  300. Set an attribute with a formatted value.
  301. .PP
  302. .nf
  303. void mxmlElementSetAttrf (
  304. mxml_node_t *node,
  305. const char *name,
  306. const char *format,
  307. ...
  308. );
  309. .fi
  310. .PP
  311. If the named attribute already exists, the value of the attribute
  312. is replaced by the new formatted string. The formatted string value is
  313. copied into the element node. This function does nothing if the node
  314. is not an element.
  315. .SS mxmlEntityAddCallback
  316. Add a callback to convert entities to Unicode.
  317. .PP
  318. .nf
  319. int mxmlEntityAddCallback (
  320. mxml_entity_cb_t cb
  321. );
  322. .fi
  323. .SS mxmlEntityGetName
  324. Get the name that corresponds to the character value.
  325. .PP
  326. .nf
  327. const char * mxmlEntityGetName (
  328. int val
  329. );
  330. .fi
  331. .PP
  332. If val does not need to be represented by a named entity, \fBNULL\fR is returned.
  333. .SS mxmlEntityGetValue
  334. Get the character corresponding to a named entity.
  335. .PP
  336. .nf
  337. int mxmlEntityGetValue (
  338. const char *name
  339. );
  340. .fi
  341. .PP
  342. The entity name can also be a numeric constant. -1 is returned if the
  343. name is not known.
  344. .SS mxmlEntityRemoveCallback
  345. Remove a callback.
  346. .PP
  347. .nf
  348. void mxmlEntityRemoveCallback (
  349. mxml_entity_cb_t cb
  350. );
  351. .fi
  352. .SS mxmlFindElement
  353. Find the named element.
  354. .PP
  355. .nf
  356. mxml_node_t * mxmlFindElement (
  357. mxml_node_t *node,
  358. mxml_node_t *top,
  359. const char *element,
  360. const char *attr,
  361. const char *value,
  362. int descend
  363. );
  364. .fi
  365. .PP
  366. The search is constrained by the name, attribute name, and value; any
  367. \fBNULL\fR names or values are treated as wildcards, so different kinds of
  368. searches can be implemented by looking for all elements of a given name
  369. or all elements with a specific attribute. The descend argument determines
  370. whether the search descends into child nodes; normally you will use
  371. \fBMXML_DESCEND_FIRST\fR for the initial search and \fBMXML_NO_DESCEND\fR
  372. to find additional direct descendents of the node. The top node argument
  373. constrains the search to a particular node's children.
  374. .SS mxmlFindPath
  375. Find a node with the given path.
  376. .PP
  377. .nf
  378. mxml_node_t * mxmlFindPath (
  379. mxml_node_t *top,
  380. const char *path
  381. );
  382. .fi
  383. .PP
  384. The "path" is a slash-separated list of element names. The name "\fI" is
  385. considered a wildcard for one or more levels of elements. For example,
  386. "foo/one/two", "bar/two/one", "\fR/one", and so forth.
  387. .PP
  388. The first child node of the found node is returned if the given node has
  389. children and the first child is a value node.
  390. .SS mxmlGetCDATA
  391. Get the value for a CDATA node.
  392. .PP
  393. .nf
  394. const char * mxmlGetCDATA (
  395. mxml_node_t *node
  396. );
  397. .fi
  398. .PP
  399. \fBNULL\fR is returned if the node is not a CDATA element.
  400. .SS mxmlGetCustom
  401. Get the value for a custom node.
  402. .PP
  403. .nf
  404. const void * mxmlGetCustom (
  405. mxml_node_t *node
  406. );
  407. .fi
  408. .PP
  409. \fBNULL\fR is returned if the node (or its first child) is not a custom
  410. value node.
  411. .SS mxmlGetElement
  412. Get the name for an element node.
  413. .PP
  414. .nf
  415. const char * mxmlGetElement (
  416. mxml_node_t *node
  417. );
  418. .fi
  419. .PP
  420. \fBNULL\fR is returned if the node is not an element node.
  421. .SS mxmlGetFirstChild
  422. Get the first child of an element node.
  423. .PP
  424. .nf
  425. mxml_node_t * mxmlGetFirstChild (
  426. mxml_node_t *node
  427. );
  428. .fi
  429. .PP
  430. \fBNULL\fR is returned if the node is not an element node or if the node
  431. has no children.
  432. .SS mxmlGetInteger
  433. Get the integer value from the specified node or its
  434. first child.
  435. .PP
  436. .nf
  437. int mxmlGetInteger (
  438. mxml_node_t *node
  439. );
  440. .fi
  441. .PP
  442. 0 is returned if the node (or its first child) is not an integer value node.
  443. .SS mxmlGetLastChild
  444. Get the last child of an element node.
  445. .PP
  446. .nf
  447. mxml_node_t * mxmlGetLastChild (
  448. mxml_node_t *node
  449. );
  450. .fi
  451. .PP
  452. \fBNULL\fR is returned if the node is not an element node or if the node
  453. has no children.
  454. .SS mxmlGetNextSibling
  455. .PP
  456. .nf
  457. mxml_node_t * mxmlGetNextSibling (
  458. mxml_node_t *node
  459. );
  460. .fi
  461. .SS mxmlGetOpaque
  462. Get an opaque string value for a node or its first child.
  463. .PP
  464. .nf
  465. const char * mxmlGetOpaque (
  466. mxml_node_t *node
  467. );
  468. .fi
  469. .PP
  470. \fBNULL\fR is returned if the node (or its first child) is not an opaque
  471. value node.
  472. .SS mxmlGetParent
  473. Get the parent node.
  474. .PP
  475. .nf
  476. mxml_node_t * mxmlGetParent (
  477. mxml_node_t *node
  478. );
  479. .fi
  480. .PP
  481. \fBNULL\fR is returned for a root node.
  482. .SS mxmlGetPrevSibling
  483. Get the previous node for the current parent.
  484. .PP
  485. .nf
  486. mxml_node_t * mxmlGetPrevSibling (
  487. mxml_node_t *node
  488. );
  489. .fi
  490. .PP
  491. \fBNULL\fR is returned if this is the first child for the current parent.
  492. .SS mxmlGetReal
  493. Get the real value for a node or its first child.
  494. .PP
  495. .nf
  496. double mxmlGetReal (
  497. mxml_node_t *node
  498. );
  499. .fi
  500. .PP
  501. 0.0 is returned if the node (or its first child) is not a real value node.
  502. .SS mxmlGetRefCount
  503. Get the current reference (use) count for a node.
  504. .PP
  505. .nf
  506. int mxmlGetRefCount (
  507. mxml_node_t *node
  508. );
  509. .fi
  510. .PP
  511. The initial reference count of new nodes is 1. Use the \fImxmlRetain\fR
  512. and \fImxmlRelease\fR functions to increment and decrement a node's
  513. reference count.
  514. \.
  515. .SS mxmlGetText
  516. Get the text value for a node or its first child.
  517. .PP
  518. .nf
  519. const char * mxmlGetText (
  520. mxml_node_t *node,
  521. int *whitespace
  522. );
  523. .fi
  524. .PP
  525. \fBNULL\fR is returned if the node (or its first child) is not a text node.
  526. The "whitespace" argument can be \fBNULL\fR.
  527. .PP
  528. Note: Text nodes consist of whitespace-delimited words. You will only get
  529. single words of text when reading an XML file with \fBMXML_TEXT\fR nodes.
  530. If you want the entire string between elements in the XML file, you MUST read
  531. the XML file with \fBMXML_OPAQUE\fR nodes and get the resulting strings
  532. using the \fImxmlGetOpaque\fR function instead.
  533. .SS mxmlGetType
  534. Get the node type.
  535. .PP
  536. .nf
  537. mxml_type_t mxmlGetType (
  538. mxml_node_t *node
  539. );
  540. .fi
  541. .PP
  542. \fBMXML_IGNORE\fR is returned if "node" is \fBNULL\fR.
  543. .SS mxmlGetUserData
  544. Get the user data pointer for a node.
  545. .PP
  546. .nf
  547. void * mxmlGetUserData (
  548. mxml_node_t *node
  549. );
  550. .fi
  551. .PP
  552. .SS mxmlIndexDelete
  553. Delete an index.
  554. .PP
  555. .nf
  556. void mxmlIndexDelete (
  557. mxml_index_t *ind
  558. );
  559. .fi
  560. .SS mxmlIndexEnum
  561. Return the next node in the index.
  562. .PP
  563. .nf
  564. mxml_node_t * mxmlIndexEnum (
  565. mxml_index_t *ind
  566. );
  567. .fi
  568. .PP
  569. You should call \fImxmlIndexReset\fR prior to using this function to get
  570. the first node in the index. Nodes are returned in the sorted order of the
  571. index.
  572. .SS mxmlIndexFind
  573. Find the next matching node.
  574. .PP
  575. .nf
  576. mxml_node_t * mxmlIndexFind (
  577. mxml_index_t *ind,
  578. const char *element,
  579. const char *value
  580. );
  581. .fi
  582. .PP
  583. You should call \fImxmlIndexReset\fR prior to using this function for
  584. the first time with a particular set of "element" and "value"
  585. strings. Passing \fBNULL\fR for both "element" and "value" is equivalent
  586. to calling \fImxmlIndexEnum\fR.
  587. .SS mxmlIndexGetCount
  588. Get the number of nodes in an index.
  589. .PP
  590. .nf
  591. int mxmlIndexGetCount (
  592. mxml_index_t *ind
  593. );
  594. .fi
  595. .PP
  596. .SS mxmlIndexNew
  597. Create a new index.
  598. .PP
  599. .nf
  600. mxml_index_t * mxmlIndexNew (
  601. mxml_node_t *node,
  602. const char *element,
  603. const char *attr
  604. );
  605. .fi
  606. .PP
  607. The index will contain all nodes that contain the named element and/or
  608. attribute. If both "element" and "attr" are \fBNULL\fR, then the index will
  609. contain a sorted list of the elements in the node tree. Nodes are
  610. sorted by element name and optionally by attribute value if the "attr"
  611. argument is not NULL.
  612. .SS mxmlIndexReset
  613. Reset the enumeration/find pointer in the index and
  614. return the first node in the index.
  615. .PP
  616. .nf
  617. mxml_node_t * mxmlIndexReset (
  618. mxml_index_t *ind
  619. );
  620. .fi
  621. .PP
  622. This function should be called prior to using \fImxmlIndexEnum\fR or
  623. \fImxmlIndexFind\fR for the first time.
  624. .SS mxmlLoadFd
  625. Load a file descriptor into an XML node tree.
  626. .PP
  627. .nf
  628. mxml_node_t * mxmlLoadFd (
  629. mxml_node_t *top,
  630. int fd,
  631. mxml_load_cb_t cb
  632. );
  633. .fi
  634. .PP
  635. The nodes in the specified file are added to the specified top node.
  636. If no top node is provided, the XML file MUST be well-formed with a
  637. single parent node like
  638. .URL ?xml ?xml
  639. for the entire file. The callback
  640. function returns the value type that should be used for child nodes.
  641. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  642. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  643. loading child (data) nodes of the specified type.
  644. .PP
  645. Note: The most common programming error when using the Mini-XML library is
  646. to load an XML file using the \fBMXML_TEXT_CALLBACK\fR, which returns inline
  647. text as a series of whitespace-delimited words, instead of using the
  648. \fBMXML_OPAQUE_CALLBACK\fR which returns the inline text as a single string
  649. (including whitespace).
  650. .SS mxmlLoadFile
  651. Load a file into an XML node tree.
  652. .PP
  653. .nf
  654. mxml_node_t * mxmlLoadFile (
  655. mxml_node_t *top,
  656. FILE *fp,
  657. mxml_load_cb_t cb
  658. );
  659. .fi
  660. .PP
  661. The nodes in the specified file are added to the specified top node.
  662. If no top node is provided, the XML file MUST be well-formed with a
  663. single parent node like
  664. .URL ?xml ?xml
  665. for the entire file. The callback
  666. function returns the value type that should be used for child nodes.
  667. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  668. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  669. loading child (data) nodes of the specified type.
  670. .PP
  671. Note: The most common programming error when using the Mini-XML library is
  672. to load an XML file using the \fBMXML_TEXT_CALLBACK\fR, which returns inline
  673. text as a series of whitespace-delimited words, instead of using the
  674. \fBMXML_OPAQUE_CALLBACK\fR which returns the inline text as a single string
  675. (including whitespace).
  676. .SS mxmlLoadString
  677. Load a string into an XML node tree.
  678. .PP
  679. .nf
  680. mxml_node_t * mxmlLoadString (
  681. mxml_node_t *top,
  682. const char *s,
  683. mxml_load_cb_t cb
  684. );
  685. .fi
  686. .PP
  687. The nodes in the specified string are added to the specified top node.
  688. If no top node is provided, the XML string MUST be well-formed with a
  689. single parent node like
  690. .URL ?xml ?xml
  691. for the entire string. The callback
  692. function returns the value type that should be used for child nodes.
  693. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  694. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  695. loading child (data) nodes of the specified type.
  696. .PP
  697. Note: The most common programming error when using the Mini-XML library is
  698. to load an XML file using the \fBMXML_TEXT_CALLBACK\fR, which returns inline
  699. text as a series of whitespace-delimited words, instead of using the
  700. \fBMXML_OPAQUE_CALLBACK\fR which returns the inline text as a single string
  701. (including whitespace).
  702. .SS mxmlNewCDATA
  703. Create a new CDATA node.
  704. .PP
  705. .nf
  706. mxml_node_t * mxmlNewCDATA (
  707. mxml_node_t *parent,
  708. const char *data
  709. );
  710. .fi
  711. .PP
  712. The new CDATA node is added to the end of the specified parent's child
  713. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  714. CDATA node has no parent. The data string must be nul-terminated and
  715. is copied into the new node. CDATA nodes currently use the
  716. \fBMXML_ELEMENT\fR type.
  717. .SS mxmlNewCustom
  718. Create a new custom data node.
  719. .PP
  720. .nf
  721. mxml_node_t * mxmlNewCustom (
  722. mxml_node_t *parent,
  723. void *data,
  724. mxml_custom_destroy_cb_t destroy
  725. );
  726. .fi
  727. .PP
  728. The new custom node is added to the end of the specified parent's child
  729. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  730. element node has no parent. \fBNULL\fR can be passed when the data in the
  731. node is not dynamically allocated or is separately managed.
  732. .SS mxmlNewElement
  733. Create a new element node.
  734. .PP
  735. .nf
  736. mxml_node_t * mxmlNewElement (
  737. mxml_node_t *parent,
  738. const char *name
  739. );
  740. .fi
  741. .PP
  742. The new element node is added to the end of the specified parent's child
  743. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  744. element node has no parent.
  745. .SS mxmlNewInteger
  746. Create a new integer node.
  747. .PP
  748. .nf
  749. mxml_node_t * mxmlNewInteger (
  750. mxml_node_t *parent,
  751. int integer
  752. );
  753. .fi
  754. .PP
  755. The new integer node is added to the end of the specified parent's child
  756. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  757. integer node has no parent.
  758. .SS mxmlNewOpaque
  759. Create a new opaque string.
  760. .PP
  761. .nf
  762. mxml_node_t * mxmlNewOpaque (
  763. mxml_node_t *parent,
  764. const char *opaque
  765. );
  766. .fi
  767. .PP
  768. The new opaque string node is added to the end of the specified parent's
  769. child list. The constant \fBMXML_NO_PARENT\fR can be used to specify that
  770. the new opaque string node has no parent. The opaque string must be nul-
  771. terminated and is copied into the new node.
  772. .SS mxmlNewOpaquef
  773. Create a new formatted opaque string node.
  774. .PP
  775. .nf
  776. mxml_node_t * mxmlNewOpaquef (
  777. mxml_node_t *parent,
  778. const char *format,
  779. ...
  780. );
  781. .fi
  782. .PP
  783. The new opaque string node is added to the end of the specified parent's
  784. child list. The constant \fBMXML_NO_PARENT\fR can be used to specify that
  785. the new opaque string node has no parent. The format string must be
  786. nul-terminated and is formatted into the new node.
  787. .SS mxmlNewReal
  788. Create a new real number node.
  789. .PP
  790. .nf
  791. mxml_node_t * mxmlNewReal (
  792. mxml_node_t *parent,
  793. double real
  794. );
  795. .fi
  796. .PP
  797. The new real number node is added to the end of the specified parent's
  798. child list. The constant \fBMXML_NO_PARENT\fR can be used to specify that
  799. the new real number node has no parent.
  800. .SS mxmlNewText
  801. Create a new text fragment node.
  802. .PP
  803. .nf
  804. mxml_node_t * mxmlNewText (
  805. mxml_node_t *parent,
  806. int whitespace,
  807. const char *string
  808. );
  809. .fi
  810. .PP
  811. The new text node is added to the end of the specified parent's child
  812. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  813. text node has no parent. The whitespace parameter is used to specify
  814. whether leading whitespace is present before the node. The text
  815. string must be nul-terminated and is copied into the new node.
  816. .SS mxmlNewTextf
  817. Create a new formatted text fragment node.
  818. .PP
  819. .nf
  820. mxml_node_t * mxmlNewTextf (
  821. mxml_node_t *parent,
  822. int whitespace,
  823. const char *format,
  824. ...
  825. );
  826. .fi
  827. .PP
  828. The new text node is added to the end of the specified parent's child
  829. list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new
  830. text node has no parent. The whitespace parameter is used to specify
  831. whether leading whitespace is present before the node. The format
  832. string must be nul-terminated and is formatted into the new node.
  833. .SS mxmlNewXML
  834. Create a new XML document tree.
  835. .PP
  836. .nf
  837. mxml_node_t * mxmlNewXML (
  838. const char *version
  839. );
  840. .fi
  841. .PP
  842. The "version" argument specifies the version number to put in the
  843. ?xml element node. If \fBNULL\fR, version "1.0" is assumed.
  844. .SS mxmlRelease
  845. Release a node.
  846. .PP
  847. .nf
  848. int mxmlRelease (
  849. mxml_node_t *node
  850. );
  851. .fi
  852. .PP
  853. When the reference count reaches zero, the node (and any children)
  854. is deleted via \fImxmlDelete\fR.
  855. .SS mxmlRemove
  856. Remove a node from its parent.
  857. .PP
  858. .nf
  859. void mxmlRemove (
  860. mxml_node_t *node
  861. );
  862. .fi
  863. .PP
  864. This function does not free memory used by the node - use \fImxmlDelete\fR
  865. for that. This function does nothing if the node has no parent.
  866. .SS mxmlRetain
  867. Retain a node.
  868. .PP
  869. .nf
  870. int mxmlRetain (
  871. mxml_node_t *node
  872. );
  873. .fi
  874. .PP
  875. .SS mxmlSAXLoadFd
  876. Load a file descriptor into an XML node tree
  877. using a SAX callback.
  878. .PP
  879. .nf
  880. mxml_node_t * mxmlSAXLoadFd (
  881. mxml_node_t *top,
  882. int fd,
  883. mxml_load_cb_t cb,
  884. mxml_sax_cb_t sax_cb,
  885. void *sax_data
  886. );
  887. .fi
  888. .PP
  889. The nodes in the specified file are added to the specified top node.
  890. If no top node is provided, the XML file MUST be well-formed with a
  891. single parent node like
  892. .URL ?xml ?xml
  893. for the entire file. The callback
  894. function returns the value type that should be used for child nodes.
  895. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  896. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  897. loading child nodes of the specified type.
  898. .PP
  899. The SAX callback must call \fImxmlRetain\fR for any nodes that need to
  900. be kept for later use. Otherwise, nodes are deleted when the parent
  901. node is closed or after each data, comment, CDATA, or directive node.
  902. .SS mxmlSAXLoadFile
  903. Load a file into an XML node tree
  904. using a SAX callback.
  905. .PP
  906. .nf
  907. mxml_node_t * mxmlSAXLoadFile (
  908. mxml_node_t *top,
  909. FILE *fp,
  910. mxml_load_cb_t cb,
  911. mxml_sax_cb_t sax_cb,
  912. void *sax_data
  913. );
  914. .fi
  915. .PP
  916. The nodes in the specified file are added to the specified top node.
  917. If no top node is provided, the XML file MUST be well-formed with a
  918. single parent node like
  919. .URL ?xml ?xml
  920. for the entire file. The callback
  921. function returns the value type that should be used for child nodes.
  922. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  923. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  924. loading child nodes of the specified type.
  925. .PP
  926. The SAX callback must call \fImxmlRetain\fR for any nodes that need to
  927. be kept for later use. Otherwise, nodes are deleted when the parent
  928. node is closed or after each data, comment, CDATA, or directive node.
  929. .SS mxmlSAXLoadString
  930. Load a string into an XML node tree
  931. using a SAX callback.
  932. .PP
  933. .nf
  934. mxml_node_t * mxmlSAXLoadString (
  935. mxml_node_t *top,
  936. const char *s,
  937. mxml_load_cb_t cb,
  938. mxml_sax_cb_t sax_cb,
  939. void *sax_data
  940. );
  941. .fi
  942. .PP
  943. The nodes in the specified string are added to the specified top node.
  944. If no top node is provided, the XML string MUST be well-formed with a
  945. single parent node like
  946. .URL ?xml ?xml
  947. for the entire string. The callback
  948. function returns the value type that should be used for child nodes.
  949. The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR,
  950. \fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for
  951. loading child nodes of the specified type.
  952. .PP
  953. The SAX callback must call \fImxmlRetain\fR for any nodes that need to
  954. be kept for later use. Otherwise, nodes are deleted when the parent
  955. node is closed or after each data, comment, CDATA, or directive node.
  956. .SS mxmlSaveAllocString
  957. Save an XML tree to an allocated string.
  958. .PP
  959. .nf
  960. char * mxmlSaveAllocString (
  961. mxml_node_t *node,
  962. mxml_save_cb_t cb
  963. );
  964. .fi
  965. .PP
  966. This function returns a pointer to a string containing the textual
  967. representation of the XML node tree. The string should be freed
  968. using \fBfree()\fR when you are done with it. \fBNULL\fR is returned if the node
  969. would produce an empty string or if the string cannot be allocated.
  970. .PP
  971. The callback argument specifies a function that returns a whitespace
  972. string or \fBNULL\fR before and after each element. If \fBMXML_NO_CALLBACK\fR
  973. is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes
  974. with leading whitespace and before attribute names inside opening
  975. element tags.
  976. .SS mxmlSaveFd
  977. Save an XML tree to a file descriptor.
  978. .PP
  979. .nf
  980. int mxmlSaveFd (
  981. mxml_node_t *node,
  982. int fd,
  983. mxml_save_cb_t cb
  984. );
  985. .fi
  986. .PP
  987. The callback argument specifies a function that returns a whitespace
  988. string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR
  989. is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes
  990. with leading whitespace and before attribute names inside opening
  991. element tags.
  992. .SS mxmlSaveFile
  993. Save an XML tree to a file.
  994. .PP
  995. .nf
  996. int mxmlSaveFile (
  997. mxml_node_t *node,
  998. FILE *fp,
  999. mxml_save_cb_t cb
  1000. );
  1001. .fi
  1002. .PP
  1003. The callback argument specifies a function that returns a whitespace
  1004. string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR
  1005. is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes
  1006. with leading whitespace and before attribute names inside opening
  1007. element tags.
  1008. .SS mxmlSaveString
  1009. Save an XML node tree to a string.
  1010. .PP
  1011. .nf
  1012. int mxmlSaveString (
  1013. mxml_node_t *node,
  1014. char *buffer,
  1015. int bufsize,
  1016. mxml_save_cb_t cb
  1017. );
  1018. .fi
  1019. .PP
  1020. This function returns the total number of bytes that would be
  1021. required for the string but only copies (bufsize - 1) characters
  1022. into the specified buffer.
  1023. .PP
  1024. The callback argument specifies a function that returns a whitespace
  1025. string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR
  1026. is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes
  1027. with leading whitespace and before attribute names inside opening
  1028. element tags.
  1029. .SS mxmlSetCDATA
  1030. Set the element name of a CDATA node.
  1031. .PP
  1032. .nf
  1033. int mxmlSetCDATA (
  1034. mxml_node_t *node,
  1035. const char *data
  1036. );
  1037. .fi
  1038. .PP
  1039. The node is not changed if it (or its first child) is not a CDATA element node.
  1040. .SS mxmlSetCustom
  1041. Set the data and destructor of a custom data node.
  1042. .PP
  1043. .nf
  1044. int mxmlSetCustom (
  1045. mxml_node_t *node,
  1046. void *data,
  1047. mxml_custom_destroy_cb_t destroy
  1048. );
  1049. .fi
  1050. .PP
  1051. The node is not changed if it (or its first child) is not a custom node.
  1052. .SS mxmlSetCustomHandlers
  1053. Set the handling functions for custom data.
  1054. .PP
  1055. .nf
  1056. void mxmlSetCustomHandlers (
  1057. mxml_custom_load_cb_t load,
  1058. mxml_custom_save_cb_t save
  1059. );
  1060. .fi
  1061. .PP
  1062. The load function accepts a node pointer and a data string and must
  1063. return 0 on success and non-zero on error.
  1064. .PP
  1065. The save function accepts a node pointer and must return a malloc'd
  1066. string on success and \fBNULL\fR on error.
  1067. .SS mxmlSetElement
  1068. Set the name of an element node.
  1069. .PP
  1070. .nf
  1071. int mxmlSetElement (
  1072. mxml_node_t *node,
  1073. const char *name
  1074. );
  1075. .fi
  1076. .PP
  1077. The node is not changed if it is not an element node.
  1078. .SS mxmlSetErrorCallback
  1079. Set the error message callback.
  1080. .PP
  1081. .nf
  1082. void mxmlSetErrorCallback (
  1083. mxml_error_cb_t cb
  1084. );
  1085. .fi
  1086. .SS mxmlSetInteger
  1087. Set the value of an integer node.
  1088. .PP
  1089. .nf
  1090. int mxmlSetInteger (
  1091. mxml_node_t *node,
  1092. int integer
  1093. );
  1094. .fi
  1095. .PP
  1096. The node is not changed if it (or its first child) is not an integer node.
  1097. .SS mxmlSetOpaque
  1098. Set the value of an opaque node.
  1099. .PP
  1100. .nf
  1101. int mxmlSetOpaque (
  1102. mxml_node_t *node,
  1103. const char *opaque
  1104. );
  1105. .fi
  1106. .PP
  1107. The node is not changed if it (or its first child) is not an opaque node.
  1108. .SS mxmlSetOpaquef
  1109. Set the value of an opaque string node to a formatted string.
  1110. .PP
  1111. .nf
  1112. int mxmlSetOpaquef (
  1113. mxml_node_t *node,
  1114. const char *format,
  1115. ...
  1116. );
  1117. .fi
  1118. .PP
  1119. The node is not changed if it (or its first child) is not an opaque node.
  1120. .SS mxmlSetReal
  1121. Set the value of a real number node.
  1122. .PP
  1123. .nf
  1124. int mxmlSetReal (
  1125. mxml_node_t *node,
  1126. double real
  1127. );
  1128. .fi
  1129. .PP
  1130. The node is not changed if it (or its first child) is not a real number node.
  1131. .SS mxmlSetText
  1132. Set the value of a text node.
  1133. .PP
  1134. .nf
  1135. int mxmlSetText (
  1136. mxml_node_t *node,
  1137. int whitespace,
  1138. const char *string
  1139. );
  1140. .fi
  1141. .PP
  1142. The node is not changed if it (or its first child) is not a text node.
  1143. .SS mxmlSetTextf
  1144. Set the value of a text node to a formatted string.
  1145. .PP
  1146. .nf
  1147. int mxmlSetTextf (
  1148. mxml_node_t *node,
  1149. int whitespace,
  1150. const char *format,
  1151. ...
  1152. );
  1153. .fi
  1154. .PP
  1155. The node is not changed if it (or its first child) is not a text node.
  1156. .SS mxmlSetUserData
  1157. Set the user data pointer for a node.
  1158. .PP
  1159. .nf
  1160. int mxmlSetUserData (
  1161. mxml_node_t *node,
  1162. void *data
  1163. );
  1164. .fi
  1165. .PP
  1166. .SS mxmlSetWrapMargin
  1167. Set the wrap margin when saving XML data.
  1168. .PP
  1169. .nf
  1170. void mxmlSetWrapMargin (
  1171. int column
  1172. );
  1173. .fi
  1174. .PP
  1175. Wrapping is disabled when "column" is 0.
  1176. .SS mxmlWalkNext
  1177. Walk to the next logical node in the tree.
  1178. .PP
  1179. .nf
  1180. mxml_node_t * mxmlWalkNext (
  1181. mxml_node_t *node,
  1182. mxml_node_t *top,
  1183. int descend
  1184. );
  1185. .fi
  1186. .PP
  1187. The descend argument controls whether the first child is considered
  1188. to be the next node. The top node argument constrains the walk to
  1189. the node's children.
  1190. .SS mxmlWalkPrev
  1191. Walk to the previous logical node in the tree.
  1192. .PP
  1193. .nf
  1194. mxml_node_t * mxmlWalkPrev (
  1195. mxml_node_t *node,
  1196. mxml_node_t *top,
  1197. int descend
  1198. );
  1199. .fi
  1200. .PP
  1201. The descend argument controls whether the previous node's last child
  1202. is considered to be the previous node. The top node argument constrains
  1203. the walk to the node's children.
  1204. .SH TYPES
  1205. .SS mxml_custom_destroy_cb_t
  1206. Custom data destructor
  1207. .PP
  1208. .nf
  1209. typedef void(*)(void *) mxml_custom_destroy_cb_t;
  1210. .fi
  1211. .SS mxml_custom_load_cb_t
  1212. Custom data load callback function
  1213. .PP
  1214. .nf
  1215. typedef int(*)(mxml_node_t *, const char *) mxml_custom_load_cb_t;
  1216. .fi
  1217. .SS mxml_custom_save_cb_t
  1218. Custom data save callback function
  1219. .PP
  1220. .nf
  1221. typedef char *(*)(mxml_node_t *) mxml_custom_save_cb_t;
  1222. .fi
  1223. .SS mxml_entity_cb_t
  1224. Entity callback function
  1225. .PP
  1226. .nf
  1227. typedef int(*)(const char *) mxml_entity_cb_t;
  1228. .fi
  1229. .SS mxml_error_cb_t
  1230. Error callback function
  1231. .PP
  1232. .nf
  1233. typedef void(*)(const char *) mxml_error_cb_t;
  1234. .fi
  1235. .SS mxml_index_t
  1236. An XML node index.
  1237. .PP
  1238. .nf
  1239. typedef struct _mxml_index_s mxml_index_t;
  1240. .fi
  1241. .SS mxml_load_cb_t
  1242. Load callback function
  1243. .PP
  1244. .nf
  1245. typedef mxml_type_t(*)(mxml_node_t *) mxml_load_cb_t;
  1246. .fi
  1247. .SS mxml_node_t
  1248. An XML node.
  1249. .PP
  1250. .nf
  1251. typedef struct _mxml_node_s mxml_node_t;
  1252. .fi
  1253. .SS mxml_save_cb_t
  1254. Save callback function
  1255. .PP
  1256. .nf
  1257. typedef const char *(*)(mxml_node_t *, int) mxml_save_cb_t;
  1258. .fi
  1259. .SS mxml_sax_cb_t
  1260. SAX callback function
  1261. .PP
  1262. .nf
  1263. typedef void(*)(mxml_node_t *, mxml_sax_event_t, void *) mxml_sax_cb_t;
  1264. .fi
  1265. .SS mxml_sax_event_t
  1266. SAX event type.
  1267. .PP
  1268. .nf
  1269. typedef enum mxml_sax_event_e mxml_sax_event_t;
  1270. .fi
  1271. .SS mxml_type_t
  1272. The XML node type.
  1273. .PP
  1274. .nf
  1275. typedef enum mxml_type_e mxml_type_t;
  1276. .fi
  1277. .SH SEE ALSO
  1278. Mini-XML Programmers Manual, https://www.msweet.org/mxml
  1279. .SH COPYRIGHT
  1280. Copyright \[co] 2003-2021 by Michael R Sweet.