summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-09-19 16:51:04 +0200
committerSilvan Jegen <s.jegen@gmail.com>2016-09-19 16:51:04 +0200
commitbccfce55f3f53256802ce51827303dce9b47ae5d (patch)
tree244354d6c18a05711056ade35f7d30c1e21f308c
parent32d8fd1d9999d9ee869deec2c7388c29257afcfe (diff)
Let's not repeat ourselves too much
-rw-r--r--sxmlc.c31
1 files 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);