从minixml 文档复制:
对于每个元素节点,您的回调函数最多会被调用四次,并带有一个指向该节点的指针和一个“where”值MXML_WS_BEFORE_OPEN
、MXML_WS_AFTER_OPEN
、MXML_WS_BEFORE_CLOSE
或MXML_WS_AFTER_CLOSE
。如果不应添加空格,则回调函数应返回,否则应返回NULL
要插入的字符串(空格、制表符、回车和换行符)。
以下空白回调可用于向 XHTML 输出添加空白,使其在标准文本编辑器中更具可读性:
const char *
whitespace_cb(mxml_node_t *node,
int where)
{
const char *name;
/*
* We can conditionally break to a new line
* before or after any element. These are
* just common HTML elements...
*/
name = mxmlGetElement(node);
if (!strcmp(name, "html") ||
!strcmp(name, "head") ||
!strcmp(name, "body") ||
!strcmp(name, "pre") ||
!strcmp(name, "p") ||
!strcmp(name, "h1") ||
!strcmp(name, "h2") ||
!strcmp(name, "h3") ||
!strcmp(name, "h4") ||
!strcmp(name, "h5") ||
!strcmp(name, "h6"))
{
/*
* Newlines before open and after
* close...
*/
if (where == MXML_WS_BEFORE_OPEN ||
where == MXML_WS_AFTER_CLOSE)
return ("\n");
}
else if (!strcmp(name, "dl") ||
!strcmp(name, "ol") ||
!strcmp(name, "ul"))
{
/*
* Put a newline before and after list
* elements...
*/
return ("\n");
}
else if (!strcmp(name, "dd") ||
!strcmp(name, "dt") ||
!strcmp(name, "li"))
{
/*
* Put a tab before <li>'s, * <dd>'s,
* and <dt>'s, and a newline after them...
*/
if (where == MXML_WS_BEFORE_OPEN)
return ("\t");
else if (where == MXML_WS_AFTER_CLOSE)
return ("\n");
}
/*
* Return NULL for no added whitespace...
*/
return (NULL);
}
要使用此回调函数,只需在调用任何保存函数时使用名称:
FILE *fp;
mxml_node_t *tree;
fp = fopen("filename.xml", "w");
mxmlSaveFile(tree, fp, whitespace_cb);
fclose(fp);