/* based on http://xmlsoft.org/tutorial/apd.html
* compile with
* gcc -Wall `pkg-config --cflags --libs libxml-2.0` -o gweather gweather.c
*/
#include
#include
xmlDocPtr
load_xml_doc (char *docname)
{
xmlDocPtr doc;
doc = xmlParseFile (docname);
if (doc == NULL)
{
fprintf (stderr,"Document not parsed successfully.\n");
return NULL;
}
return doc;
}
xmlXPathObjectPtr
getnodeset (xmlDocPtr doc,
xmlChar *xpath)
{
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext (doc);
if (context == NULL)
{
fprintf (stderr, "Error in xmlXPathNewContext\n");
return NULL;
}
result = xmlXPathEvalExpression (xpath, context);
xmlXPathFreeContext (context);
if (result == NULL)
{
fprintf (stderr, "Error in xmlXPathEvalExpression\n");
return NULL;
}
if (xmlXPathNodeSetIsEmpty (result->nodesetval))
{
xmlXPathFreeObject (result);
fprintf (stderr, "No result\n");
return NULL;
}
return result;
}
int
main (int argc, char *argv[])
{
char *docname;
xmlDocPtr doc;
xmlChar *xpath = (xmlChar*) "//forecast_conditions/condition/@data";
xmlNodeSetPtr nodeset;
xmlXPathObjectPtr result;
int i;
xmlChar *keyword;
if (argc <= 1)
{
fprintf (stderr, "Usage: %s \n", argv[0]);
return 0;
}
docname = argv[1];
if (argc >= 3)
xpath = (xmlChar*) argv[2];
doc = load_xml_doc (docname);
result = getnodeset (doc, xpath);
if (result)
{
nodeset = result->nodesetval;
for (i = 0; i < nodeset->nodeNr; i++)
{
keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
printf ("forecast condition: %s\n", keyword);
xmlFree (keyword);
}
xmlXPathFreeObject (result);
}
xmlFreeDoc (doc);
xmlCleanupParser ();
return 1;
}