Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * section: Tree |
| 3 | * synopsis: Navigates a tree to print element names |
| 4 | * purpose: Parse a file to a tree, use xmlDocGetRootElement() to |
| 5 | * get the root element, then walk the document and print |
| 6 | * all the element name in document order. |
| 7 | * usage: tree1 filename_or_URL |
Daniel Richard G | 6842ee8 | 2012-08-17 09:58:38 +0800 | [diff] [blame] | 8 | * test: tree1 test2.xml > tree1.tmp && diff tree1.tmp $(srcdir)/tree1.res |
Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 9 | * author: Dodji Seketeli |
| 10 | * copy: see Copyright for the status of this software. |
| 11 | */ |
| 12 | #include <stdio.h> |
| 13 | #include <libxml/parser.h> |
| 14 | #include <libxml/tree.h> |
| 15 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 16 | #ifdef LIBXML_TREE_ENABLED |
| 17 | |
Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 18 | /* |
| 19 | *To compile this file using gcc you can type |
| 20 | *gcc `xml2-config --cflags --libs` -o xmlexample libxml2-example.c |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * print_element_names: |
| 25 | * @a_node: the initial xml node to consider. |
| 26 | * |
| 27 | * Prints the names of the all the xml elements |
| 28 | * that are siblings or children of a given xml node. |
| 29 | */ |
William M. Brack | 60f394e | 2003-11-16 06:25:42 +0000 | [diff] [blame] | 30 | static void |
Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 31 | print_element_names(xmlNode * a_node) |
| 32 | { |
| 33 | xmlNode *cur_node = NULL; |
| 34 | |
| 35 | for (cur_node = a_node; cur_node; cur_node = cur_node->next) { |
| 36 | if (cur_node->type == XML_ELEMENT_NODE) { |
| 37 | printf("node type: Element, name: %s\n", cur_node->name); |
| 38 | } |
| 39 | |
| 40 | print_element_names(cur_node->children); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Simple example to parse a file called "file.xml", |
| 47 | * walk down the DOM, and print the name of the |
| 48 | * xml elements nodes. |
| 49 | */ |
| 50 | int |
| 51 | main(int argc, char **argv) |
| 52 | { |
| 53 | xmlDoc *doc = NULL; |
| 54 | xmlNode *root_element = NULL; |
| 55 | |
| 56 | if (argc != 2) |
| 57 | return(1); |
| 58 | |
| 59 | /* |
| 60 | * this initialize the library and check potential ABI mismatches |
| 61 | * between the version it was compiled for and the actual shared |
| 62 | * library used. |
| 63 | */ |
| 64 | LIBXML_TEST_VERSION |
| 65 | |
| 66 | /*parse the file and get the DOM */ |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 67 | doc = xmlReadFile(argv[1], NULL, 0); |
Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 68 | |
| 69 | if (doc == NULL) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 70 | printf("error: could not parse file %s\n", argv[1]); |
Daniel Veillard | d9d63d6 | 2003-11-13 11:45:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /*Get the root element node */ |
| 74 | root_element = xmlDocGetRootElement(doc); |
| 75 | |
| 76 | print_element_names(root_element); |
| 77 | |
| 78 | /*free the document */ |
| 79 | xmlFreeDoc(doc); |
| 80 | |
| 81 | /* |
| 82 | *Free the global variables that may |
| 83 | *have been allocated by the parser. |
| 84 | */ |
| 85 | xmlCleanupParser(); |
| 86 | |
| 87 | return 0; |
| 88 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 89 | #else |
| 90 | int main(void) { |
| 91 | fprintf(stderr, "Tree support not compiled in\n"); |
| 92 | exit(1); |
| 93 | } |
| 94 | #endif |