/
xml.c
79 lines (60 loc) · 1.45 KB
/
xml.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Serge Voilokov, 2015
* helpers for libxml2.
*/
#include "xml.h"
#include <string.h>
#include <stdbool.h>
#include <err.h>
#include <libxml/tree.h>
const char *
get_ctext(const xmlNodePtr node)
{
xmlNodePtr n;
if (node == NULL)
return NULL;
for (n = node->children; n != NULL; n = n->next)
if (n->type == XML_TEXT_NODE)
return (char *)n->content;
return NULL;
}
const char *
get_attr(const xmlNodePtr node, const char *name)
{
if (node == NULL && node->type != XML_ELEMENT_NODE)
return NULL;
xmlNodePtr n = (xmlNodePtr)((xmlElementPtr)node)->attributes;
for (; n != NULL; n = n->next)
if (n->type == XML_ATTRIBUTE_NODE && xmlStrEqual(n->name, BAD_CAST(name)) == 1)
return get_ctext(n);
return NULL;
}
xmlNodePtr
first_el(xmlNodePtr parent_node, const char *name)
{
xmlNodePtr n;
if (parent_node == NULL)
return NULL;
for (n = parent_node->children; n != NULL; n = n->next)
if (n->type == XML_ELEMENT_NODE && xmlStrEqual(n->name, BAD_CAST(name)) == 1)
return n;
return NULL;
}
xmlNodePtr
next_el(xmlNodePtr node)
{
xmlNodePtr n;
for (n = node->next; n != NULL; n = n->next)
if (n->type == XML_ELEMENT_NODE && xmlStrEqual(n->name, node->name) == 1)
return n;
return NULL;
}
void
print_element_names(xmlNode *a_node, int depth)
{
xmlNode *n = NULL;
for (n = a_node; n != NULL; n = n->next) {
if (n->type == XML_ELEMENT_NODE)
printf("%*s%s\n", depth*4, "", n->name);
print_element_names(n->children, depth+1);
}
}