-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2csv.h
125 lines (100 loc) · 1.89 KB
/
xml2csv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
void parse_element_names(xmlNode * a_node);
void xmlToCsv(char *out);
void taketags(xmlNode * node);
void takecontents(xmlNode * node);
xmlNode* tags[256];
xmlNode* data[256];
static void convertXmltoCsv(char *in, char *out)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
doc = xmlReadFile(in, NULL, XML_PARSE_NOBLANKS);
if (doc == NULL)
{
printf("error: could not parse file %s\n", in);
}
else
{
/* Get the root element node*/
root_element = xmlDocGetRootElement(doc);
parse_element_names(root_element);
xmlToCsv(out);
/*free the document*/
xmlFreeDoc(doc);
}
/*Free the global variables that may have been allocated by the parser.*/
xmlCleanupParser();
}
/* Recursive function that prints the XML structure */
void taketags(xmlNode * node)
{
int i = 0, flag = 0;
while(tags[i])
{
if(strcmp(tags[i]->name, node->name) == 0)
{
flag=1;
}
i++;
}
if(flag == 0)
{
tags[i] = node;
}
i = 0;
}
void takecontents(xmlNode * node)
{
int i = 0;
while(data[i])
{
i++;
}
data[i] = node;
i = 0;
}
void xmlToCsv(char *out)
{
FILE *fp = fopen(out,"w");
int i = 0, j = 0;
while(tags[i])
{
if(tags[i + 1] == NULL)
{
fprintf(fp, "%s\n", tags[i]->name);
}
else
{
fprintf(fp, "%s,", tags[i]->name);
}
i++;
}
while(data[j])
{
j++;
if(j % i == 0)
{
fprintf(fp, "%s\n", data[j-1]->children->content);
}
else
{
fprintf(fp, "%s,", data[j-1]->children->content);
}
}
fclose(fp);
}
void parse_element_names(xmlNode * a_node)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE && cur_node->children->content != NULL) {
taketags(cur_node);
takecontents(cur_node);
}
parse_element_names(cur_node->children);
}
}