#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sxml.h>

#define	TAG_ROOT	"server"
#define	ROOT_ATTR_NAME	NULL
#define	ROOT_ATTR_VALUE	NULL
#define	TAG_ADDRESS	"address"
#define	TAG_PORT	"port"

static void	parse(sxml_node_t * node);

int main(int argc, char * argv[])
{
  int	fd;

  if ((fd = open("ex.xml", O_RDONLY, 0400)) != -1) {
    sxml_node_t *	root;

    if ((root = sxml_parse_file(fd)) != NULL) {
      parse(root);
      sxml_delete_node(root);
    }
    close(fd);
  }

  return 0;
}

static void
parse(sxml_node_t * root)
{
  sxml_node_t *	node;

  node = sxml_find_element(root, TAG_ROOT, ROOT_ATTR_NAME, ROOT_ATTR_VALUE);
  if (node != NULL) {
    sxml_node_t *	np;

    for (np = sxml_get_child(node); np != NULL;
	 np = sxml_get_next_sibling(np)) {
      const char *	content;

      if (sxml_get_type(np) != SXML_ELEMENT) { continue; }
      if (sxml_get_element_name(np) == NULL) { continue; }

      content = sxml_get_content(sxml_get_child(np));
      if (strcmp(sxml_get_element_name(np), TAG_ADDRESS) == 0) {
	fprintf(stdout, "address: %s\n", content);
      }
      else if (strcmp(sxml_get_element_name(np), TAG_PORT) == 0) {
	fprintf(stdout, "port: %s\n", content);
      }
      else {
	fprintf(stderr, "WARN: unknown tag: %s\n", sxml_get_element_name(np));
      }
    }
  }
}
