Update README.md examples.

This commit is contained in:
Michael R Sweet 2024-03-20 11:32:06 -04:00
parent db39074de2
commit d54a88db43
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244

View File

@ -87,26 +87,24 @@ Mini-XML provides a single header file which you include:
#include <mxml.h> #include <mxml.h>
Nodes (elements, comments, processing directives, integers, opaque strings, real Nodes (elements, comments, declarations, integers, opaque strings, processing
numbers, and text strings) are represented by `mxml_node_t` objects. New nodes instructions, real numbers, and text strings) are represented by `mxml_node_t`
can be created using the mxmlNewComment, mxmlNewCustom, mxmlNewDeclaration, pointers. New nodes can be created using the mxmlNewXxx functions. The top
mxmlNewDirective, mxmlNewElement, mxmlNewInteger, mxmlNewOpaque, mxmlNewReal, node must be the `<?xml ...?>` processing instruction.
and mxmlNewText functions. The top node must be the "?xml ...?" processing
instruction.
You load an XML file using the mxmlLoadFilename function: You load an XML file using the mxmlLoadFilename function:
mxml_node_t *tree; mxml_node_t *tree;
tree = mxmlLoadFilename(NULL, "filename.xml", tree = mxmlLoadFilename(/*top*/NULL, /*options*/NULL,
/*load_cb*/NULL, /*load_cbdata*/NULL); "example.xml");
Similarly, you save an XML file using the mxmlSaveFilename function: Similarly, you save an XML file using the mxmlSaveFilename function:
mxml_node_t *tree; mxml_node_t *tree;
mxmlSaveFilename(tree, "filename.xml", mxmlSaveFilename(tree, /*options*/NULL,
/*load_cb*/NULL, /*load_cbdata*/NULL); "filename.xml");
There are variations of these functions for loading from or saving to file There are variations of these functions for loading from or saving to file
descriptors, `FILE` pointers, strings, and IO callbacks. descriptors, `FILE` pointers, strings, and IO callbacks.
@ -114,38 +112,37 @@ descriptors, `FILE` pointers, strings, and IO callbacks.
You can find a named element/node using the mxmlFindElement function: You can find a named element/node using the mxmlFindElement function:
mxml_node_t *node = mxmlFindElement(tree, tree, "name", "attr", mxml_node_t *node = mxmlFindElement(tree, tree, "name", "attr",
"value", MXML_DESCEND); "value", MXML_DESCEND_ALL);
The `name`, `attr`, and `value` arguments can be passed as `NULL` to act as The `name`, `attr`, and `value` arguments can be passed as `NULL` to act as
wildcards, e.g.: wildcards, e.g.:
/* Find the first "a" element */ /* Find the first "a" element */
node = mxmlFindElement(tree, tree, "a", NULL, NULL, MXML_DESCEND); node = mxmlFindElement(tree, tree, "a", NULL, NULL, MXML_DESCEND_ALL);
/* Find the first "a" element with "href" attribute */ /* Find the first "a" element with "href" attribute */
node = mxmlFindElement(tree, tree, "a", "href", NULL, MXML_DESCEND); node = mxmlFindElement(tree, tree, "a", "href", NULL, MXML_DESCEND_ALL);
/* Find the first "a" element with "href" to a URL */ /* Find the first "a" element with "href" to a URL */
node = mxmlFindElement(tree, tree, "a", "href", node = mxmlFindElement(tree, tree, "a", "href",
"http://www.minixml.org/", "https://www.msweet.org/mxml", MXML_DESCEND_ALL);
MXML_DESCEND);
/* Find the first element with a "src" attribute*/ /* Find the first element with a "src" attribute*/
node = mxmlFindElement(tree, tree, NULL, "src", NULL, MXML_DESCEND); node = mxmlFindElement(tree, tree, NULL, "src", NULL, MXML_DESCEND_ALL);
/* Find the first element with a "src" = "foo.jpg" */ /* Find the first element with a "src" = "foo.jpg" */
node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg", node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg",
MXML_DESCEND); MXML_DESCEND_ALL);
You can also iterate with the same function: You can also iterate with the same function:
mxml_node_t *node; mxml_node_t *node;
for (node = mxmlFindElement(tree, tree, "name", NULL, NULL, for (node = mxmlFindElement(tree, tree, "name", NULL, NULL,
MXML_DESCEND); MXML_DESCEND_ALL);
node != NULL; node != NULL;
node = mxmlFindElement(node, tree, "name", NULL, NULL, node = mxmlFindElement(node, tree, "name", NULL, NULL,
MXML_DESCEND)) MXML_DESCEND_ALL))
{ {
... do something ... ... do something ...
} }
@ -166,7 +163,7 @@ retrieve the corresponding value from a node:
double realvalue = mxmlGetReal(node); double realvalue = mxmlGetReal(node);
int whitespacevalue; bool whitespacevalue;
const char *textvalue = mxmlGetText(node, &whitespacevalue); const char *textvalue = mxmlGetText(node, &whitespacevalue);
Finally, once you are done with the XML data, use the mxmlDelete function to Finally, once you are done with the XML data, use the mxmlDelete function to