免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7247 | 回复: 2
打印 上一主题 下一主题

[SCO UNIX] libxml2里如何实现xml的对齐的格式化输出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-28 17:00 |只看该作者 |倒序浏览
文档里说的怎么不起效果呢。
新增节点都是连在一起的
Function: xmlDocDumpFormatMemory
void        xmlDocDumpFormatMemory                (xmlDocPtr cur,                                          xmlChar ** mem,                                          int * size,                                          int format)
Dump an XML document in memory and return the #xmlChar * and it's size. It's up to the caller to free the memory with xmlFree(). Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called

cur: the document
mem: OUT: the memory pointer
size: OUT: the memory length
format: should formatting spaces been added

Function: xmlSaveFormatFileEnc
int        xmlSaveFormatFileEnc                (const char * filename,                                          xmlDocPtr cur,                                          const char * encoding,                                          int format)
Dump an XML document to a file or an URL.

filename: the filename or URL to output
cur: the document being saved
encoding: the name of the encoding to use or NULL.
format: should formatting spaces be added.
Returns: the number of bytes written or -1 in case of error. Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called

[ 本帖最后由 wenlq 于 2008-2-28 17:02 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-03-03 17:24 |只看该作者
经过测试可以通过了。

xmlSaveCtxtPtr 那种方式是xmllint --format 时采用的方式。



  1.     #include <stdio.h>

  2.     #include <string.h>

  3.     #include <iconv.h>
  4.     #include <libxml/tree.h>
  5.     #include <libxml/xmlsave.h>
  6.     #include <libxml/parser.h>
  7.     #include <libxml/xpath.h>
  8.     #include <libxml/xpathInternals.h>

  9. int main(int argc,char** argv)
  10. {
  11.     xmlDocPtr doc;
  12.     xmlSaveCtxtPtr ctxt;
  13.     int saveOpts = 0;

  14.     if(argc<2)
  15.     {
  16.      printf("argc must be 2 or above.\n");
  17.      return -1;
  18.     }
  19.     xmlKeepBlanksDefault(0);

  20.     doc = xmlParseFile(argv[1]);
  21.     if (doc == NULL)
  22.     {
  23.         printf("Error: unable to parse file: [%s]\n",argv[1]);
  24.         return -1;
  25.     }
  26.     xmlSaveFormatFileEnc("ll",doc,doc->encoding,1);

  27.     saveOpts |= XML_SAVE_FORMAT;
  28.     ctxt = xmlSaveToFilename(argv[2] ? argv[2]:"-", doc->encoding, saveOpts);
  29.     if (ctxt != NULL) {
  30.         if (xmlSaveDoc(ctxt, doc) < 0) {
  31.             fprintf(stderr, "failed save to %s\n", argv[2]);
  32.         }
  33.         xmlSaveClose(ctxt);
  34.     }
  35.     else {
  36.             fprintf(stderr, "failed save to %s\n", argv[2]);
  37.     }

  38.     xmlFreeDoc(doc);
  39.     xmlCleanupParser();
  40.     return 0;

  41. }


复制代码

评分

参与人数 1可用积分 +5 收起 理由
sdccf + 5 原创内容

查看全部评分

论坛徽章:
0
3 [报告]
发表于 2008-03-14 14:16 |只看该作者
补充一点,新增节点时,

  <hello>
    <nn1>
      <OKKK>Oktest</OKKK>
      <what>what ....what </what>
    </nn1>
    <nn2>hellotext2</nn2>
    <nn3>hellotext3</nn3>
  </hello>
新增节点hello,nn1,以下函数的最后参数content要用NULL,不能用空串“”。否则就变连一起了。

  1. <hello><nn1><OKKK>Oktest</OKKK><what>what ....what </what></nn1><nn2>hellotext2</nn2><nn3>hellotext3</nn3></hello>
复制代码


xmlNodePtr xmlNewChild(xmlNodePtr parent,  xmlNsPtr ns,
const xmlChar * name, const xmlChar * content)

[ 本帖最后由 wenlq 于 2008-3-14 14:18 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP