页首Html代码

返回顶部

Linux C语言调用libxml2的库文件。格式化xml文件,让rootElement的闭合标签新起一行!

好久没有更新了.其实这周在写东西,东西没出来,就没有写博客了.

刚才发现一个超级恶心的问题.

我用libxml读取xml数据,然后插入一个数据。但是居然是一行,而没有新起一行。怎么办呢,我就搜libxml format 和xmlSaveFormatFile ,还是有不少这样的问题的。还有人说这是一个bug,其实很恶心的很好笑的。

在libxml2的 $PREFIX/share/gtk-doc/html/libxml2/libxml2-tree.html#xmlSaveFormatFileEnc 有这样的一个解释.

xmlSaveFormatFile ()

int xmlSaveFormatFile (const char * filename, xmlDocPtr cur, int format)

Dump an XML document to a file. Will use compression if compiled in and enabled. If @filename is "-" the stdout file is used.
If @format is set then the document will be indented on output.
 Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called。

filename: the filename (or URL)
cur: the document
format: should formatting spaces been added
Returns: the number of bytes written or -1 in case of failure.

最后一个参数是format,=1的时候就可以format,但是,当 下面两个全局变量 值被设置 才管用。

xmlKeepBlanksDefault(0) ;//libxml2 global variable .
xmlIndentTreeOutput = 1 ;// indent .with \n

有这两个参数并进行设置才可以管用。在xmlSaveFormatFile前面加入这两行,运行不管用。

然后搜索。最后看到一个说用xmlReadFile而不是xmlParseFile解析文件。

doc1 = xmlParseFile(docname);
//doc1 = xmlReadFile(docname, NULL, XML_PARSE_NOBLANKS);

发现貌似还不管用。后来发现,在这个解析xml文件前面加上 上面的两个全局变量,终于有换行了。而且还自动格式化了xml文件。不管用那个都管用。

所以这不是一个bug,而是一个调用先后的问题。但是上面的函数 解释的,一点也没有申明这一点。所以,反正现在可以格式化xml文件了,很好的。。

 附上一些我搜索到的一些资料链接:

http://www.cppblog.com/lymons/archive/2009/03/30/37553.html 

http://westsoftware.blog.163.com/blog/static/2609410920072175245763/ 这里有一个中文版本的文档.示例很重要.有了示例好办事情了.

posted @ 2011-12-16 18:22  ayanmw  阅读(6394)  评论(1编辑  收藏  举报

页脚Html代码