This document is simply a very short example illustrating
the practical use of XML, and describing the few most important
functions provided by the libxml library. This document is based
on a tutorial I have found on the DeveloperWorks web site.
Consider the following XML data :
<?xml version="1.0"?> <Genealogy> <Person name="Vaclav I." dob="After 905" dod="28.9. 929 or 935">tata</Person> <Person name="Vratislav I." dob="Unknown" dod="13.2. 921"></Person> <Person name="Borivoj I." dob="Unknown" dod="Around 894" /> <Person name="Ludmila" dob="Unknown" dod="16.9. 921" /> <Person name="Drahomir" dob="Unknown" dod="Unknown" /> </Genealogy>
#include <gnome-xml/tree.h> #include <gnome-xml/parser.h> int main(int argc, char** argv) { xmlDocPtr doc; xmlNodePtr node; char *name; char *info; if (argc != 2) { printf("Please provide a file name\n"); exit(0); } doc = xmlParseFile(argv[1]); if(!doc->root || !doc->root->name || strcasecmp(doc->root->name,"Genealogy")!=0) { xmlFreeDoc(doc); } for(node = doc->root->childs; node != NULL; node = node->next) { name = xmlGetProp(node,"name"); info = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); printf("Name=%s", name); if (info) printf(", info=%s\n", info); else printf("\n"); } return 0; }
cc -o domxml domxml.c -lxml