From 88872e990a1fa226cca5521d6b89640bea0dfb88 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 12 Sep 2016 21:13:43 +0200 Subject: Print article-title text --- sxmlc.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/sxmlc.c b/sxmlc.c index 6272a22..f56811a 100644 --- a/sxmlc.c +++ b/sxmlc.c @@ -1,8 +1,11 @@ #include #include +#include #include "sxmlclib.h" +void print_article_title(XMLNode *root); + int process(char *fn) { XMLDoc *doc = malloc(sizeof(XMLDoc)); if (!doc) { @@ -18,6 +21,8 @@ int process(char *fn) { return 1; } + // Apparently ill-formed XML is not an error but the library + // can't find the root node afterwards... if (doc->i_root < 0) { fprintf(stderr, "i_root was negative. Skipping file '%s'.\n", fn); return 1; @@ -28,19 +33,62 @@ int process(char *fn) { fprintf(stderr, "Root was NULL. Exiting.\n"); return 1; } - //get_article_title(root); - - fprintf(stderr, "root is '%s'.\n", root->tag); - + print_article_title(root); XMLDoc_free(doc); return 0; } -XMLNode* get_article_title(XMLNode *root) { +XMLNode* find_child_node(XMLNode *node, char* tagname) { + XMLNode** children = node->children; + XMLNode* next = NULL; + + for (int i = 0; i < node->n_children; i++) { + if (!strcmp(children[i]->tag, tagname)) { + next = children[i]; + break; + } + } + + return next; +} + +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; + } + + 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"); + return; + } + + printf("article-title: %s\n", next->text); - return root; + // XMLNode** children = next->children; + // for (int i = 0; i < next->n_children; i++) { + // if (children[i]->tag_type == TAG_TEXT) { + // printf("printing piecewise: %s", children[i]->text); + // } + // } } int main(int argc, char *argv[]) { -- cgit v1.2.3