From 66ce09e09770b5e802eea6c76140f601494a0fc1 Mon Sep 17 00:00:00 2001 From: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Wed, 31 Oct 2012 20:45:03 +0100 Subject: [PATCH] Added a usage example --- CMakeLists.txt | 11 ++++++-- test/example.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 test/example.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d36f39..8405456 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,14 @@ ADD_EXECUTABLE(test-xml TARGET_LINK_LIBRARIES(test-xml xml) +# Building example +ADD_EXECUTABLE(example + ${TEST_SOURCE_DIRECTORY}/example +) +TARGET_LINK_LIBRARIES(example xml) + + # Deploy -INSTALL(TARGETS xml DESTINATION lib) -INSTALL(FILES DESTINATION) +#INSTALL(TARGETS xml DESTINATION lib) +#INSTALL(FILES DESTINATION) diff --git a/test/example.c b/test/example.c new file mode 100644 index 0000000..05980ea --- /dev/null +++ b/test/example.c @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2012 ooxi/xml.c + * https://github.com/ooxi/xml.c + * + * This software is provided 'as-is', without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from the + * use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in a + * product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source distribution. + */ +#include +#include +#include +#include + + + +int main(int argc, char** argv) { + + /* XML source, could be read from disk + */ + uint8_t* source = "" + "" + "World" + "" + ":-)" + ":-O" + ":-D" + "" + "" + ; + + + /* Parse the document + * + * @warning Remember not to free the source until you have freed the + * document itself. If you have to free the source before, supply a + * copy to xml_parse_document which can be freed together with the + * document (`free_buffer' argument to `xml_document_free') + */ + struct xml_document* document = xml_parse_document(source, strlen(source)); + + /* You _have_ to check the result of `xml_parse_document', if it's 0 + * then the source could not be parsed. If you think this is a bug in + * xml.c, than use a debug build (cmake -DCMAKE_BUILD_TYPE=Debug) which + * will verbosely tell you about the parsing process + */ + if (!document) { + printf("Could parse document\n"); + exit(EXIT_FAILURE); + } + struct xml_node* root = xml_document_root(document); + + + /* Extract amount of Root/This children + */ + struct xml_node* root_this = xml_node_child(root, 1); + printf("Root/This has %lu children\n", (unsigned long)xml_node_children(root_this)); + + + /* Remember to free the document or you'll risk a memory leak + */ + xml_document_free(document, false); +} +