Update quick API overview in README.

This commit is contained in:
Michael R Sweet 2024-03-16 22:31:17 -04:00
parent 143fa436ca
commit 496bcdd21b
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244

View File

@ -87,46 +87,29 @@ Mini-XML provides a single header file which you include:
Nodes (elements, comments, processing directives, integers, opaque strings, real Nodes (elements, comments, processing directives, integers, opaque strings, real
numbers, and text strings) are represented by `mxml_node_t` objects. New nodes numbers, and text strings) are represented by `mxml_node_t` objects. New nodes
can be created using the `mxmlNewElement()`, `mxmlNewInteger()`, can be created using the mxmlNewComment, mxmlNewCustom, mxmlNewDeclaration,
`mxmlNewOpaque()`, `mxmlNewReal()`, and `mxmlNewText()` functions. Only mxmlNewDirective, mxmlNewElement, mxmlNewInteger, mxmlNewOpaque, mxmlNewReal,
elements can have child nodes, and the top node must be the "?xml" processing and mxmlNewText functions. The top node must be the "?xml ...?" processing
directive. instruction.
You load an XML file using the `mxmlLoadFile()` function: You load an XML file using the mxmlLoadFilename function:
FILE *fp;
mxml_node_t *tree; mxml_node_t *tree;
fp = fopen("filename.xml", "r"); tree = mxmlLoadFilename(NULL, "filename.xml",
tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK); /*load_cb*/NULL, /*load_cbdata*/NULL);
fclose(fp);
Similarly, you save an XML file using the `mxmlSaveFile()` function: Similarly, you save an XML file using the mxmlSaveFilename function:
FILE *fp;
mxml_node_t *tree; mxml_node_t *tree;
fp = fopen("filename.xml", "w"); mxmlSaveFilename(tree, "filename.xml",
mxmlSaveFile(tree, fp, MXML_NO_CALLBACK); /*load_cb*/NULL, /*load_cbdata*/NULL);
fclose(fp);
The `mxmlLoadString()`, `mxmlSaveAllocString()`, and `mxmlSaveString()` There are variations of these functions for loading from or saving to file
functions load XML node trees from and save XML node trees to strings: descriptors, `FILE` pointers, strings, and IO callbacks.
char buffer[8192]; You can find a named element/node using the mxmlFindElement function:
char *ptr;
mxml_node_t *tree;
...
tree = mxmlLoadString(NULL, buffer, MXML_OPAQUE_CALLBACK);
...
mxmlSaveString(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK);
...
ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
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);
@ -165,13 +148,13 @@ You can also iterate with the same function:
... do something ... ... do something ...
} }
The `mxmlFindPath()` function finds the (first) value node under a specific The mxmlFindPath function finds the (first) value node under a specific
element using an XPath: element using an XPath:
mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar"); mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar");
The `mxmlGetInteger()`, `mxmlGetOpaque()`, `mxmlGetReal()`, and The mxmlGetInteger, mxmlGetOpaque, mxmlGetReal, and mxmlGetText functions
`mxmlGetText()` functions retrieve the corresponding value from a node: retrieve the corresponding value from a node:
mxml_node_t *node; mxml_node_t *node;
@ -184,9 +167,9 @@ The `mxmlGetInteger()`, `mxmlGetOpaque()`, `mxmlGetReal()`, and
int whitespacevalue; int 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()` Finally, once you are done with the XML data, use the mxmlDelete function to
function to recursively free the memory that is used for a particular node recursively free the memory that is used for a particular node or the entire
or the entire tree: tree:
mxmlDelete(tree); mxmlDelete(tree);
@ -209,7 +192,7 @@ files "LICENSE" and "NOTICE" for more information.
> Note: The exception listed in the NOTICE file only applies when linking > Note: The exception listed in the NOTICE file only applies when linking
> against GPL2/LGPL2-only software. Some Apache License purists have objected > against GPL2/LGPL2-only software. Some Apache License purists have objected
> to linking Apa/che Licensed code against Mini-XML with these exceptions on the > to linking Apache Licensed code against Mini-XML with these exceptions on the
> grounds that it makes Mini-XML somehow incompatible with the Apache License. > grounds that it makes Mini-XML somehow incompatible with the Apache License.
> For that reason, people wishing to retain their Apache License purity may > For that reason, people wishing to retain their Apache License purity may
> omit the exception from their copy of Mini-XML. > omit the exception from their copy of Mini-XML.