C语言格式化xml
来源:互联网 发布:excel中mid函数数组 编辑:程序博客网 时间:2024/06/05 16:55
网上有许多关于xml格式和json格式解析的开源库
json:https://github.com/DaveGamble/cJSON
xml:http://xmlsoft.org/downloads.html
下面是我写的C输出xml格式的简单案例,希望大神们多多指导
main.c如下
#include "xml.h"int main(int argc, char *argv[]){ char buff[4096] = {'\0'}; if (!set_path(buff)) { XZ_DEBUG("set_path error"); return 0; } put_head("utf-8"); root_tag_start("sheelbook", NULL); array_tag_start("book", "id=\"p1\""); item_tag("title", "神雕侠侣"); item_tag("author", "金庸"); array_tag_end("book"); array_tag_start("book", "id=\"p2\""); item_tag("title", "射雕英雄传"); item_tag("author", "金庸"); array_tag_end("book"); array_tag_start("book", "id=\"p3\""); item_tag("title", "倚天屠龙记"); item_tag("author", "金庸"); array_tag_end("book"); root_tag_end("sheelbook"); printf("%s", buff); return 0;}
xml的具体函数如下
#include "xml.h"//输出位置static char *out_file_path = NULL;//总共有几个tagstatic int tag_number = 0;/* * 描述: * 设置输出路径 * * 参数: * format:输出格式 * 如果是FILE型,则后面参数接路径 * * 返回值: * 设置是否成功 * */bool set_path(char *path){ bool flag = false; if (path != NULL) { out_file_path = path; flag = true; } return flag;}/* * * 描述: * 设置xml头 * 参数: * format:编码格式 * * 返回值: * none */void put_head(char *format){ strcat(out_file_path, "<?"); strcat(out_file_path, "xml version=\"1.0\" encoding="); strcat(out_file_path, "\""); strcat(out_file_path, format); strcat(out_file_path, "\""); strcat(out_file_path, "?>\n");}/* * 描述: * 设置根结点 * * 参数: * tag:根结点的名称 * value:附加属性值 * * 返回值: * none */void root_tag_start(char *tag, char *value){ int n = 0; tag_number += 1; strcat(out_file_path, "<"); strcat(out_file_path, tag); if (value != NULL) { strcat(out_file_path, " "); strcat(out_file_path, value); } strcat(out_file_path, ">"); strcat(out_file_path, "\n");}/* * 描述: * 设置子结点 * * 参数: * tag:子节点名称 * value:子节点属性值 * * 返回值: * none * */void array_tag_start(char *tag, char *value){ int n = 0; //当前tag是第n个tag,就给前面加n-1个Tab符 for (n = 0; n < tag_number; n++) { strcat(out_file_path, "\t"); } strcat(out_file_path, "<"); strcat(out_file_path, tag); if (value != NULL) { strcat(out_file_path, " "); strcat(out_file_path, value); } strcat(out_file_path, ">"); strcat(out_file_path, "\n");}/* * 描述: * 设置标签值 * * 参数: * tag:标签名称 * value:标签值 * * 返回值: * none * */void item_tag(char *tag, char *value){ int n = 0; for (n = 0; n <= tag_number; n++) { strcat(out_file_path, "\t"); } strcat(out_file_path, "<"); strcat(out_file_path, tag); strcat(out_file_path, ">"); strcat(out_file_path, value); strcat(out_file_path, "</"); strcat(out_file_path, tag); strcat(out_file_path, ">"); strcat(out_file_path, "\n");}/* * 描述: * 设置子节点结束符 * * 参数: * tag:子节点名称 * * 返回值: * none * */void array_tag_end(char *tag){ int n = 0; //当前value是第n个tag,就给前面加n个Tab符 for (n = 0; n < tag_number; n++) { strcat(out_file_path, "\t"); } strcat(out_file_path, "</"); strcat(out_file_path, tag); strcat(out_file_path, ">\n");}/* * 描述: * 设置根节点结束符 * * 参数: * tag:根节点名称 * * 返回值: * none * */void root_tag_end(char *tag){ int n = 0; //当前value是第n个tag,就给前面加n个Tab符 for (n = 0; n < tag_number - 1; n++) { strcat(out_file_path, "\t"); } strcat(out_file_path, "</"); strcat(out_file_path, tag); strcat(out_file_path, ">\n"); //成功,则tag_number减一 tag_number -= 1;}
xml头文件如下
#ifndef __XZ_XMLLIB_H__#define __XZ_XMLLIB_H__#include <stdio.h>#include <string.h>#include <stdbool.h>//#define _XZ_DEBUG#ifdef _XZ_DEBUG#define XZ_DEBUG(format, ...) printf(format"\n", ##__VA_ARGS__)#else#define XZ_DEBUG(format, ...)#endifbool set_path(char *path);void put_head(char *format);void root_tag_start(char *tag, char *value);void array_tag_start(char *tag, char *value);void item_tag(char *tag, char *value);void array_tag_end(char *tag);void root_tag_end(char *tag);#endif
最后的输出结果如下
<?xml version="1.0" encoding="UTF-8"?><sheelbook> <book id="p1"> <title>神雕侠侣</title> <author>金庸</author> </book> <book id="p2"> <title>射雕英雄传</title> <author>金庸</author> </book> <book id="p3"> <title>新白娘子传奇</title> <author>古龙</author> </book></sheelbook>
0 0
- C语言格式化xml
- c xml 格式化对齐
- C语言格式化字符串
- C语言的格式化
- C语言格式化输出
- C语言格式化输出
- C语言%格式化参数
- C语言 格式化
- C语言格式化输出
- C语言格式化IO
- 格式化输出 C语言
- C语言格式化输入输出
- C语言格式化输入输出
- C语言格式化输入输出
- C 语言格式化输出
- C语言格式化输出
- C语言数据格式化
- C语言格式化说明符
- ICE简单介绍及使用示例
- 记录工作中可能用到的--3 数据之间的转化
- iOS 基于AsyncSocket的二次开发
- php复习 第十天 图形操作1之文字水印
- 45度斜角地图菱形坐标转换
- C语言格式化xml
- CF
- 线程同步
- 正则验证手机号
- 欢迎使用CSDN-markdown编辑器
- 机器学习算法需要注意的一些问题,算法一些问题
- php复习 第十天 图形操作2之图形水印
- 构建用户画像:5W2H法(二)
- Android 6.0 运行时权限封装框架