File size: 2,465 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*=============================================================================
                               parse_xml
===============================================================================
  This is an example of using the XML parsing facilities of
  XML-RPC For C/C++, in particular its libxmlrpc_xmlparse library.

  Feed XML (any XML - doesn't have to be XML-RPC) to this program as
  standard input.  It parses it and tells you what it sees.

  Example:

    $ ./gen_sample_add_xml | ./parse_xml

=============================================================================*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "xmlrpc-c/base.h"
#include "xmlrpc-c/xmlparser.h"



static const char *
getStdin(void) {

    size_t const maxInputSize = 64 * 1024;
    char * retval;

    retval = malloc(maxInputSize + 1);

    if (retval == NULL) {
        fprintf(stderr, "malloc failed.");
        exit(100);
    } else {
        size_t rc;

        rc = fread(retval, 1, maxInputSize, stdin);

        if (rc < 0)
            fprintf(stderr, "Failed to read Standard Input");
        else {
            size_t const bytesRead = rc;
            retval[bytesRead] = '\0';
        }
    }
    return retval;
}



static void
describeXmlElement(const xml_element * const elemP,
                   const char *        const prefix) {

    unsigned int i;

    printf("%sXML element type:         '%s'\n",
           prefix, xml_element_name(elemP));

    printf("%sCdata:                    '%s'\n",
           prefix, xml_element_cdata(elemP));

    printf("%sNumber of child elements: %u\n",
           prefix, xml_element_children_size(elemP));

    if (xml_element_children_size(elemP) == 0)
        printf("\n");

    for (i = 0; i < xml_element_children_size(elemP); ++i) {
        char * const newPrefix = malloc(strlen(prefix) + 2);
        sprintf(newPrefix, "%s  ", prefix);
        describeXmlElement(xml_element_children(elemP)[i], newPrefix);
        free(newPrefix);
    }
}



int
main(int argc, const char ** argv) {

    const char * xml;
    xmlrpc_env env;
    xml_element * elemP;

    xmlrpc_env_init(&env);

    xml = getStdin();

    xml_parse(&env, xml, strlen(xml), &elemP);

    if (env.fault_occurred)
        printf("xml_parse failed.  %s\n", env.fault_string);
    else {
        printf("Parsed successfully.\n");

        describeXmlElement(elemP, "");
    }
    xmlrpc_env_clean(&env);

    return 0;
}