转自: http://blog.sina.com.cn/s/blog_4ab2ba570100iria.html
以下是对minixml分析后做出的例子。希望对大家有用。至于minixml库,大家可google查找。
.....2...
.....3..
....4.
...5.
....8....
-------
-----
-----
-----
-----
-----
#include"mxml.h"
#inclide
#include
#include
const char *
whitespace_cb(mxml_node_t *node,
int where)
{
mxml_node_t *parent;
int level;
const char *name;
static const char *tabs = "\t\t\t\t\t\t\t\t";
name = node->value.element.name;
if (!strcmp(name, "schedule") || !strcmp(name, "record") || !strcmp(name, "space"))
{
if (!strcmp(name, "schedule")||where == MXML_WS_BEFORE_OPEN || where == MXML_WS_AFTER_CLOSE)
return("\n");
else(!strcmp(name, "record") || !strcmp(name, "space") ||where == MXML_WS_BEFORE_OPEN)
return("\t");
else(!strcmp(name, "record") || !strcmp(name, "space") ||where == MXML_WS_AFTER_CLOSE)
return("\n");
}
else if (!strcmp(name, "dd") || !strcmp(name, "dt") || !strcmp(name, "li"))
{
if (where == MXML_WS_BEFORE_OPEN)
return ("\t");
else if (where == MXML_WS_AFTER_CLOSE)
return ("\n");
}
else if (!strncmp(name, "?xml", 4))
{
if (where == MXML_WS_AFTER_OPEN)
return ("\n");
else
return (NULL);
}
else if (where == MXML_WS_BEFORE_OPEN ||
((!strcmp(name, "choice") || !strcmp(name, "option")) &&
where == MXML_WS_BEFORE_CLOSE))
{
for (level = -1, parent = node->parent;
parent;level ++, parent = parent->parent);
if (level > 8)
level = 8;
else if (level < 0)
level = 0;
return (tabs + 8 - level);
}
else if (where == MXML_WS_AFTER_CLOSE ||
((!strcmp(name, "group") || !strcmp(name, "option") ||
!strcmp(name, "choice")) &&
where == MXML_WS_AFTER_OPEN))
return ("\n");
else if (where == MXML_WS_AFTER_OPEN && !node->child)
return ("\n");
return (NULL);
}
int main()
{
mxml_node_t *xml;
mxml_node_t *schedule;
mxml_node_t *record;
mxml_node_t *space;
mxml_node_t *node;
xml = mxmlNewXML("1.0");
schedule = mxmlNewElement(xml, "schedule");
for(i=0;i<2;i++)
{
record = mxmlNewElement(schedule, "record");
node = mxmlNewElement(record, "program_content");
mxmlNewText(node, 0, "val4");
node = mxmlNewElement(record, "from");
mxmlNewText(node, 0, "val5");
node = mxmlNewElement(record, "to");
mxmlNewText(node, 0, "val6");
node = mxmlNewElement(record, "mode");
mxmlNewText(node, 0, "val6");
node = mxmlNewElement(record, "frequency");
mxmlNewText(node, 0, "val6");
node = mxmlNewElement(record, "pre_set_from");
mxmlNewText(node, 0, "val6");
}
space = mxmlNewElement(schedule, "space");
node = mxmlNewElement(space, "FHD");
mxmlNewText(node, 0, "val4");
node = mxmlNewElement(space, "SHD");
mxmlNewText(node, 0, "val5");
node = mxmlNewElement(space, "MAC");
mxmlNewText(node, 0, mac_address());
FILE *fp;
fp = fopen("filename.xml", "w");
mxmlSaveFile(xml, fp,whitespace_cb );
fclose(fp);
mxmlDelete(xml);
}
///////////////// ///////////////////////
////////////////////////////////////////////
parse XML example
5000
#include"mxml.h"
#include
#include
#include
int main()
{
FILE *fp;
mxml_node_t *tree,node;
fp = fopen("filename.xml", "r");
tree = mxmlLoadFile(NULL, fp,MXML_TEXT_CALLBACK);
fclose(fp);
mxml_node_t *id,*password;
node = mxmlFindElement(tree, tree, "note",
NULL, NULL,
MXML_DESCEND);
id = mxmlFindElement(node, tree, "id",
NULL, NULL,
MXML_NO_DESCEND);
printf("[%s}\n",id->child->value.text.string);
password = mxmlFindElement(node, tree, "password",
NULL, NULL,
MXML_NO_DESCEND);
printf("[%s]\n",password->child->value.text.string);
mxmlDelete(tree);
}