#include #include #include #include "sxmlclib.h" void print_article_title(XMLNode *root); int process(char *fn) { XMLDoc *doc = malloc(sizeof(XMLDoc)); if (!doc) { fprintf(stderr, "Could not allocate memory for the XMLDoc when trying to parse %s.\n", fn); exit(1); } XMLDoc_init(doc); int ret = XMLDoc_parse_file_DOM(fn, doc); if (!ret) { fprintf(stderr, "Error when parsing file '%s'.\n", 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; } XMLNode *root = doc->nodes[doc->i_root]; if (!root) { fprintf(stderr, "Root was NULL. Exiting.\n"); return 1; } print_article_title(root); XMLDoc_free(doc); return 0; } XMLNode* find_child_node(XMLNode *node, const 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 = root; static const char *path[] = {"front", "article-meta", "title-group", "article-title", NULL}; 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); } int main(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { process(argv[i]); } return 0; }