Blame doc/examples/tree1.c

Packit 423ecb
/**
Packit 423ecb
 * section: Tree
Packit 423ecb
 * synopsis: Navigates a tree to print element names
Packit 423ecb
 * purpose: Parse a file to a tree, use xmlDocGetRootElement() to
Packit 423ecb
 *          get the root element, then walk the document and print
Packit 423ecb
 *          all the element name in document order.
Packit 423ecb
 * usage: tree1 filename_or_URL
Packit 423ecb
 * test: tree1 test2.xml > tree1.tmp && diff tree1.tmp $(srcdir)/tree1.res
Packit 423ecb
 * author: Dodji Seketeli
Packit 423ecb
 * copy: see Copyright for the status of this software.
Packit 423ecb
 */
Packit 423ecb
#include <stdio.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_TREE_ENABLED
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 *To compile this file using gcc you can type
Packit 423ecb
 *gcc `xml2-config --cflags --libs` -o xmlexample libxml2-example.c
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * print_element_names:
Packit 423ecb
 * @a_node: the initial xml node to consider.
Packit 423ecb
 *
Packit 423ecb
 * Prints the names of the all the xml elements
Packit 423ecb
 * that are siblings or children of a given xml node.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
print_element_names(xmlNode * a_node)
Packit 423ecb
{
Packit 423ecb
    xmlNode *cur_node = NULL;
Packit 423ecb
Packit 423ecb
    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
Packit 423ecb
        if (cur_node->type == XML_ELEMENT_NODE) {
Packit 423ecb
            printf("node type: Element, name: %s\n", cur_node->name);
Packit 423ecb
        }
Packit 423ecb
Packit 423ecb
        print_element_names(cur_node->children);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * Simple example to parse a file called "file.xml", 
Packit 423ecb
 * walk down the DOM, and print the name of the 
Packit 423ecb
 * xml elements nodes.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
main(int argc, char **argv)
Packit 423ecb
{
Packit 423ecb
    xmlDoc *doc = NULL;
Packit 423ecb
    xmlNode *root_element = NULL;
Packit 423ecb
Packit 423ecb
    if (argc != 2)
Packit 423ecb
        return(1);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * this initialize the library and check potential ABI mismatches
Packit 423ecb
     * between the version it was compiled for and the actual shared
Packit 423ecb
     * library used.
Packit 423ecb
     */
Packit 423ecb
    LIBXML_TEST_VERSION
Packit 423ecb
Packit 423ecb
    /*parse the file and get the DOM */
Packit 423ecb
    doc = xmlReadFile(argv[1], NULL, 0);
Packit 423ecb
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        printf("error: could not parse file %s\n", argv[1]);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*Get the root element node */
Packit 423ecb
    root_element = xmlDocGetRootElement(doc);
Packit 423ecb
Packit 423ecb
    print_element_names(root_element);
Packit 423ecb
Packit 423ecb
    /*free the document */
Packit 423ecb
    xmlFreeDoc(doc);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     *Free the global variables that may
Packit 423ecb
     *have been allocated by the parser.
Packit 423ecb
     */
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
#else
Packit 423ecb
int main(void) {
Packit 423ecb
    fprintf(stderr, "Tree support not compiled in\n");
Packit 423ecb
    exit(1);
Packit 423ecb
}
Packit 423ecb
#endif