new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_xml / examples / test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "os_xml.h"
6
7
8 int main(int argc, char **argv)
9 {
10     int i = 0;
11     OS_XML xml;
12     XML_NODE node = NULL;
13
14     /* File name must be given */
15     if (argc < 2) {
16         printf("Usage: %s file\n", argv[0]);
17         return (-1);
18     }
19
20     /* Read the XML. Print error and line number */
21     if (OS_ReadXML(argv[1], &xml) < 0) {
22         printf("OS_ReadXML error: %s, line :%d\n", xml.err, xml.err_line);
23         return (1);
24     }
25
26     if (OS_ApplyVariables(&xml) != 0) {
27         printf("OS_ReadXML error: Applying variables: %s\n", xml.err);
28         return (1);
29     }
30
31     /* Get all nodes */
32     node = OS_GetElementsbyNode(&xml, NULL);
33     if (node == NULL) {
34         printf("OS_GetElementsbyNode error: %s, line: %d\n", xml.err, xml.err_line);
35         return (1);
36     }
37
38     i = 0;
39
40     while (node[i]) {
41         int j = 0;
42         XML_NODE cnode;
43
44         cnode = OS_GetElementsbyNode(&xml, node[i]);
45         if (cnode == NULL) {
46             i++;
47             continue;
48         }
49
50         while (cnode[j]) {
51             printf("Element: %s -> %s\n",
52                    cnode[j]->element,
53                    cnode[j]->content);
54             if (cnode[j]->attributes && cnode[j]->values) {
55                 int k = 0;
56                 while (cnode[j]->attributes[k]) {
57                     printf("attr %s:%s\n",
58                            cnode[j]->attributes[k],
59                            cnode[j]->values[k]);
60                     k++;
61                 }
62             }
63             j++;
64         }
65
66         OS_ClearNode(cnode);
67         i++;
68     }
69
70     /* Clear the nodes */
71     OS_ClearNode(node);
72
73     node = NULL;
74
75     OS_ClearXML(&xml);
76
77     return (0);
78 }
79