File: parser.c

package info (click to toggle)
libnxml 0.18.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 428 kB
  • sloc: ansic: 3,225; makefile: 45; sh: 33
file content (94 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (3)
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
/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini
 *                                       <baku@ippolita.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "nxml.h"
#include <stdarg.h>

void my_printf(char *str, ...) {
  va_list va;

  va_start(va, str);
  vfprintf(stderr, str, va);
  va_end(va);
}

int main(int argc, char **argv) {
  nxml_t *data;
  nxml_error_t ret;
  nxml_data_t *element, *tmp;
  nxml_doctype_t *doctype;

  if (argc != 2) {
    fprintf(stderr,
            "Usage:\n\t%s url_xml\n\nExample:\n\t%s "
            "[file.xml|http://server/file.xml]\n\n",
            argv[0], argv[0]);
    return 1;
  }

  ret = nxml_new(&data);

  if (ret != NXML_OK) {
    puts(nxml_strerror(data, ret));
    return 1;
  }

  ret = nxml_set_func(data, my_printf);

  if (ret != NXML_OK) {
    puts(nxml_strerror(data, ret));
    return 1;
  }

  if (!strncmp(argv[1], "http://", 7) || !strncmp(argv[1], "https://", 8))
    ret = nxml_parse_url(data, argv[1]);
  else
    ret = nxml_parse_file(data, argv[1]);

  if (ret != NXML_OK) {
    puts(nxml_strerror(data, ret));
    return 1;
  }

  nxml_root_element(data, &element);
  printf("Root element: %p: -%s-\n", (void *)element,
         element ? element->value : "");

  if (element) {
    tmp = element->children;
    while (tmp) {
      printf("  subelement: %p: -%s-\n", (void *)tmp, tmp ? tmp->value : "");
      tmp = tmp->next;
    }
  }

  nxml_find_element(data, element, "body", &element);
  if (element && nxml_find_element(data, element, "div", &element) == NXML_OK &&
      element) {
    printf("\ndiv element: -%p-\n", (void *)element);
  }

  nxml_doctype_element(data, &doctype);

  if (doctype)
    printf("\nDoctype: -%s-%s-\n", doctype->name, doctype->value);

  nxml_free(data);

  return 0;
}