- 论坛徽章:
- 0
|
经过测试可以通过了。
xmlSaveCtxtPtr 那种方式是xmllint --format 时采用的方式。
- #include <stdio.h>
- #include <string.h>
- #include <iconv.h>
- #include <libxml/tree.h>
- #include <libxml/xmlsave.h>
- #include <libxml/parser.h>
- #include <libxml/xpath.h>
- #include <libxml/xpathInternals.h>
- int main(int argc,char** argv)
- {
- xmlDocPtr doc;
- xmlSaveCtxtPtr ctxt;
- int saveOpts = 0;
- if(argc<2)
- {
- printf("argc must be 2 or above.\n");
- return -1;
- }
- xmlKeepBlanksDefault(0);
- doc = xmlParseFile(argv[1]);
- if (doc == NULL)
- {
- printf("Error: unable to parse file: [%s]\n",argv[1]);
- return -1;
- }
- xmlSaveFormatFileEnc("ll",doc,doc->encoding,1);
- saveOpts |= XML_SAVE_FORMAT;
- ctxt = xmlSaveToFilename(argv[2] ? argv[2]:"-", doc->encoding, saveOpts);
- if (ctxt != NULL) {
- if (xmlSaveDoc(ctxt, doc) < 0) {
- fprintf(stderr, "failed save to %s\n", argv[2]);
- }
- xmlSaveClose(ctxt);
- }
- else {
- fprintf(stderr, "failed save to %s\n", argv[2]);
- }
- xmlFreeDoc(doc);
- xmlCleanupParser();
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|