Imported Upstream version 2.7
[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 int main(int argc, char ** argv)
8 {
9     int i = 0;
10     OS_XML xml;
11     XML_NODE node = NULL;
12
13
14     /* File name must be given */
15     if(argc < 2)
16     {
17         printf("Usage: %s file\n",argv[0]);
18         return(-1);
19     }
20
21
22     /* Reading the XML. Printing error and line number */
23     if(OS_ReadXML(argv[1],&xml) < 0)
24     {
25         printf("OS_ReadXML error: %s, line :%d\n",xml.err, xml.err_line);
26         return(1);
27     }
28
29     if(OS_ApplyVariables(&xml) != 0)
30     {
31         printf("OS_ReadXML error: Applying variables: %s\n", xml.err);
32         return(1);
33     }
34
35     /* Getting all nodes */
36     node = OS_GetElementsbyNode(&xml,NULL);
37     if(node == NULL)
38     {
39         printf("OS_GetElementsbyNode error: %s, line: %d\n", xml.err, xml.err_line);
40         return(1);
41     }
42
43     i = 0;
44
45     while(node[i])
46     {
47         int j = 0;
48         XML_NODE cnode;
49
50         cnode = OS_GetElementsbyNode(&xml, node[i]);
51         if(cnode == NULL)
52         {
53             i++;
54             continue;
55         }
56
57         while(cnode[j])
58         {
59             printf("Element: %s -> %s\n",
60                     cnode[j]->element,
61                     cnode[j]->content);
62             if(cnode[j]->attributes && cnode[j]->values)
63             {
64                 int k = 0;
65                 while(cnode[j]->attributes[k])
66                 {
67                     printf("attr %s:%s\n",
68                            cnode[j]->attributes[k],
69                            cnode[j]->values[k]);
70                     k++;
71                 }
72             }
73             j++;
74         }
75
76         OS_ClearNode(cnode);
77         i++;
78     }
79
80     /* Clearing the nodes */
81     OS_ClearNode(node);
82
83     node = NULL;
84
85     OS_ClearXML(&xml);
86
87     return(0);
88 }