瀏覽代碼

Added a usage example

master
ooxi 12 年之前
父節點
當前提交
66ce09e097
共有 2 個文件被更改,包括 86 次插入2 次删除
  1. +9
    -2
      CMakeLists.txt
  2. +77
    -0
      test/example.c

+ 9
- 2
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)

+ 77
- 0
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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <xml.h>



int main(int argc, char** argv) {

/* XML source, could be read from disk
*/
uint8_t* source = ""
"<Root>"
"<Hello>World</Hello>"
"<This>"
"<Is>:-)</Is>"
"<An>:-O</An>"
"<Example>:-D</Example>"
"</This>"
"</Root>"
;


/* 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);
}


Loading…
取消
儲存