使用libxml2时,可以获得更多控制格式
(复制+从在这里):
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
/*
*To compile this file using gcc you can type
*gcc `xml2-config --cflags --libs` -o tree2 tree2.c
*/
/* A simple example how to create DOM. Libxml2 automagically
* allocates the necessary amount of memory to it.
*/
int
main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
char buff[256];
int i, j;
LIBXML_TEST_VERSION;
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
xmlNewChild(root_node, NULL, BAD_CAST "node1",
BAD_CAST "content of node 1");
xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);
node =
xmlNewChild(root_node, NULL, BAD_CAST "node3",
BAD_CAST "this node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST
"other way to create content (which is also a node)");
xmlAddChild(node, node1);
xmlAddChild(root_node, node);
for (i = 5; i < 7; i++) {
sprintf(buff, "node%d", i);
node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
for (j = 1; j < 4; j++) {
sprintf(buff, "node%d%d", i, j);
node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
xmlNewProp(node1, BAD_CAST "odd", BAD_CAST((j % 2) ? "no" : "yes"));
}
}
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
return(0);
}
我有此代码 我只有两个空间缩进,我需要控制使用多少个空间,或者是否使用标签用于缩进。
我发现
I have this code (Copied+ a bit minified from here):
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
/*
*To compile this file using gcc you can type
*gcc `xml2-config --cflags --libs` -o tree2 tree2.c
*/
/* A simple example how to create DOM. Libxml2 automagically
* allocates the necessary amount of memory to it.
*/
int
main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
char buff[256];
int i, j;
LIBXML_TEST_VERSION;
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
xmlNewChild(root_node, NULL, BAD_CAST "node1",
BAD_CAST "content of node 1");
xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);
node =
xmlNewChild(root_node, NULL, BAD_CAST "node3",
BAD_CAST "this node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST
"other way to create content (which is also a node)");
xmlAddChild(node, node1);
xmlAddChild(root_node, node);
for (i = 5; i < 7; i++) {
sprintf(buff, "node%d", i);
node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
for (j = 1; j < 4; j++) {
sprintf(buff, "node%d%d", i, j);
node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
xmlNewProp(node1, BAD_CAST "odd", BAD_CAST((j % 2) ? "no" : "yes"));
}
}
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
return(0);
}
It gives me the output, but only indented by two spaces, I need to control how many spaces are used or if tabs are used for indenting.
I found this page for saving, but was not able to find any function allowing me to control the indentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在找到了它:
我必须做,例如
缩进标签或
缩进四个空间
I found it now:
I have to do e.g.
to indent with tab or
to indent with four spaces