From bccfce55f3f53256802ce51827303dce9b47ae5d Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 19 Sep 2016 16:51:04 +0200 Subject: Let's not repeat ourselves too much --- sxmlc.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/sxmlc.c b/sxmlc.c index 995ac8e..eee2f26 100644 --- a/sxmlc.c +++ b/sxmlc.c @@ -40,7 +40,7 @@ int process(char *fn) { return 0; } -XMLNode* find_child_node(XMLNode *node, char* tagname) { +XMLNode* find_child_node(XMLNode *node, const char* tagname) { XMLNode** children = node->children; XMLNode* next = NULL; @@ -55,30 +55,15 @@ XMLNode* find_child_node(XMLNode *node, char* tagname) { } void print_article_title(XMLNode *root) { - XMLNode* next = NULL; - - next = find_child_node(root, "front"); - if (!next) { - fprintf(stderr, "Could not find front tag.\n"); - return; - } + XMLNode* next = root; + const char *path[] = {"front", "article-meta", "title-group", "article-title", NULL}; - next = find_child_node(next, "article-meta"); - if (!next) { - fprintf(stderr, "Could not find article-meta tag.\n"); - return; - } - - next = find_child_node(next, "title-group"); - if (!next) { - fprintf(stderr, "Could not find title-group tag.\n"); - return; - } - - next = find_child_node(next, "article-title"); - if (!next) { - fprintf(stderr, "Could not find article-title tag.\n"); + for (int i = 0; path[i]; i++) { + next = find_child_node(next, path[i]); + if (!next) { + fprintf(stderr, "Could not find '%s' tag.\n", path[i]); return; + } } printf("article-title: %s\n", next->text); -- cgit v1.2.1-18-gbd029