-
Notifications
You must be signed in to change notification settings - Fork 1
/
libxml2-xinclude.c
62 lines (52 loc) · 1.51 KB
/
libxml2-xinclude.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>
static void parse(xmlNodePtr n, int depth) {
char tmp[1024];
memset(tmp, ' ', depth);
tmp[depth] = '\0';
for (xmlAttrPtr a = n->properties; a; a = a->next) {
if (a->type == XML_ATTRIBUTE_NODE) {
xmlNodePtr valPtr = a->children;
printf("%s[%s]=[%s]\n", tmp, a->name, valPtr->content);
}
}
for (xmlNodePtr c = n->children; c; c = c->next) {
if (c->type == XML_ELEMENT_NODE) {
printf("%s[%s]\n", tmp, c->name);
parse(c, depth + 1);
}
}
}
int main(int argc, char *argv[]) {
LIBXML_TEST_VERSION
{
xmlDocPtr doc = xmlReadFile(argv[1], NULL, 0);
if (doc) {
int rc = xmlXIncludeProcess(doc);
printf("xmlXIncludeProcess => %d\n", rc);
if (rc < 0) {
fprintf(stderr, "XInclude processing failed\n");
exit(1);
} else {
xmlNodePtr root = xmlDocGetRootElement(doc);
if (root) {
parse(root, 0);
} else {
fprintf(stderr, "xmlDocGetRootElement failed\n");
exit(1);
}
}
xmlFreeDoc(doc);
} else {
fprintf(stderr, "xmlReadFile failed\n");
exit(1);
}
}
xmlCleanupParser();
printf("Bye\n");
return 0;
}