From 933d3f31965010d90cb09759a00f86618097f0a4 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 12 Sep 2016 18:28:09 +0200 Subject: Make sure we are printing the right tag We have to check that we are in the title-group tag before getting the tag's contents. --- yxml.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/yxml.c b/yxml.c index 1fd0f51..d16d070 100644 --- a/yxml.c +++ b/yxml.c @@ -43,7 +43,7 @@ buffer *buffer_append(buffer *buf, char *data) { int process(char *fn, yxml_t *state) { size_t filelen; char *parsebuffer; - int inelement; + int intitlegroup, inarticletitle; buffer *contentbuf; FILE* f = fopen(fn, "rb"); @@ -74,7 +74,9 @@ int process(char *fn, yxml_t *state) { fclose(f); - inelement = 0; + intitlegroup = 0; + inarticletitle = 0; + contentbuf = buffer_new(); yxml_init(state, state+1, BUFSIZE); @@ -82,8 +84,6 @@ int process(char *fn, yxml_t *state) { for (; *parsebuffer; parsebuffer++) { yxml_ret_t r = yxml_parse(state, *parsebuffer); - //fprintf(stderr, "parse_state: %d\n", r); - if(r < 0) { printf("Parsing error at %s: line %u, byte %lu, offset %lu\n", fn, state->line, state->byte, state->total); @@ -92,22 +92,26 @@ int process(char *fn, yxml_t *state) { switch(r) { case YXML_ELEMSTART: - if (!strcmp(state->elem, "article-title")) { - printf("start: %s\n", state->elem); - inelement = 1; + if (!strcmp(state->elem, "title-group")) { + intitlegroup = 1; + } else if (!strcmp(state->elem, "article-title") && intitlegroup) { + printf("%s: ", state->elem); + inarticletitle = 1; } break; case YXML_CONTENT: - if (inelement) { + if (inarticletitle) { buffer_append(contentbuf, state->data); } break; case YXML_ELEMEND: - printf("elementend: %s\n", state->elem); - if (inelement) { - inelement = 0; + // Since article-title should not be nested, + // we don't have to check the element name here. + if (inarticletitle && intitlegroup) { + inarticletitle = 0; + intitlegroup = 0; printf("%s\n", contentbuf->buffer); buffer_reset(contentbuf); } -- cgit v1.2.1-18-gbd029