diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..c03af38904bd63ba850bcadcb3bb0bac9dcfe9d1 --- /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 a2cdae425f892161f9510ad000bd86de811b3139..d932f659e46009f3603c26b70891948f448033b8 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 579446b65392a7f00b2a4a60d396afccf4a7099c..513bfd2762065a637e6355a706473d125813356b 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