Blame doc/examples/tree1.c

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