From c13fdc46d076e56e024533055df586af8e9b1c1b Mon Sep 17 00:00:00 2001 From: Orestis Malaspinas <orestis.malaspinas@hesge.ch> Date: Wed, 6 May 2020 14:30:02 +0200 Subject: [PATCH] updated files --- main.c | 16 ++++++++++++++++ xml_parser.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ xml_parser.h | 2 ++ 3 files changed, 62 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..c03af38 --- /dev/null +++ b/main.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <string.h> +#include <libxml/parser.h> +#include <libxml/tree.h> + +#include "xml_parser.h" + + +int main() { + + char filename[] = "villes.xml"; + + read_xml(filename); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/xml_parser.c b/xml_parser.c index a2cdae4..d932f65 100644 --- a/xml_parser.c +++ b/xml_parser.c @@ -44,3 +44,47 @@ void graph_xml_parser(xmlNode * a_node) } } +int read_xml(const char * filename) { + + // Init libxml + xmlInitParser(); + LIBXML_TEST_VERSION + + // create a parser context + xmlParserCtxtPtr ctxt; + xmlDocPtr doc = NULL; + ctxt = xmlNewParserCtxt(); + + if (ctxt == NULL) { + fprintf(stderr, "Failed to allocate parser context\n"); + return EXIT_FAILURE; + } + + // parse the file, without error validation + doc = xmlCtxtReadFile(ctxt, filename, NULL, XML_PARSE_NOERROR); + + // check if parsing suceeded + if (doc == NULL) { + fprintf(stderr, "Failed to parse %s\n", filename); + } else { + /* check if validation suceeded */ + if (ctxt->valid == 0) + fprintf(stderr, "Failed to validate %s\n", filename); + } + + xmlNode *root_element = xmlDocGetRootElement(doc); + + // get cities from xml + city_xml_parser(root_element); + + // get graph from xml + graph_xml_parser(root_element); + + // free the document + xmlFreeDoc(doc); + // free up the parser context + xmlFreeParserCtxt(ctxt); + + return EXIT_SUCCESS; + +} \ No newline at end of file diff --git a/xml_parser.h b/xml_parser.h index 579446b..513bfd2 100644 --- a/xml_parser.h +++ b/xml_parser.h @@ -24,4 +24,6 @@ void ville_xml_parser(xmlNode * a_node); */ void graph_xml_parser(xmlNode * a_node); +int read_xml(const char * filename); + #endif -- GitLab