MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 1 | <![CDATA[ |
| 2 | #include <libxml/parser.h> |
| 3 | #include <libxml/xpath.h> |
| 4 | |
| 5 | xmlDocPtr |
| 6 | getdoc (char *docname) { |
| 7 | xmlDocPtr doc; |
| 8 | doc = xmlParseFile(docname); |
| 9 | |
| 10 | if (doc == NULL ) { |
| 11 | fprintf(stderr,"Document not parsed successfully. \n"); |
| 12 | return NULL; |
| 13 | } |
| 14 | |
| 15 | return doc; |
| 16 | } |
| 17 | |
| 18 | xmlXPathObjectPtr |
| 19 | getnodeset (xmlDocPtr doc, xmlChar *xpath){ |
| 20 | |
| 21 | xmlXPathContextPtr context; |
| 22 | xmlXPathObjectPtr result; |
| 23 | |
| 24 | context = xmlXPathNewContext(doc); |
MDT 2004 John Fleck | 4c3bb7d | 2004-08-25 02:51:27 +0000 | [diff] [blame] | 25 | if (context == NULL) { |
| 26 | printf("Error in xmlXPathNewContext\n"); |
| 27 | return NULL; |
| 28 | } |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 29 | result = xmlXPathEvalExpression(xpath, context); |
MDT 2004 John Fleck | 4c3bb7d | 2004-08-25 02:51:27 +0000 | [diff] [blame] | 30 | xmlXPathFreeContext(context); |
| 31 | if (result == NULL) { |
| 32 | printf("Error in xmlXPathEvalExpression\n"); |
| 33 | return NULL; |
| 34 | } |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 35 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
MDT 2004 John Fleck | 4c3bb7d | 2004-08-25 02:51:27 +0000 | [diff] [blame] | 36 | xmlXPathFreeObject(result); |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 37 | printf("No result\n"); |
| 38 | return NULL; |
| 39 | } |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 40 | return result; |
| 41 | } |
| 42 | int |
| 43 | main(int argc, char **argv) { |
| 44 | |
| 45 | char *docname; |
| 46 | xmlDocPtr doc; |
MDT 2004 John Fleck | 218ed39 | 2004-05-28 01:49:38 +0000 | [diff] [blame] | 47 | xmlChar *xpath = (xmlChar*) "//keyword"; |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 48 | xmlNodeSetPtr nodeset; |
| 49 | xmlXPathObjectPtr result; |
| 50 | int i; |
| 51 | xmlChar *keyword; |
| 52 | |
| 53 | if (argc <= 1) { |
| 54 | printf("Usage: %s docname\n", argv[0]); |
| 55 | return(0); |
| 56 | } |
| 57 | |
| 58 | docname = argv[1]; |
| 59 | doc = getdoc(docname); |
| 60 | result = getnodeset (doc, xpath); |
| 61 | if (result) { |
| 62 | nodeset = result->nodesetval; |
| 63 | for (i=0; i < nodeset->nodeNr; i++) { |
| 64 | keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); |
| 65 | printf("keyword: %s\n", keyword); |
MST 2004 John Fleck | d14bccc | 2004-02-15 01:57:42 +0000 | [diff] [blame] | 66 | xmlFree(keyword); |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 67 | } |
MST 2004 John Fleck | d14bccc | 2004-02-15 01:57:42 +0000 | [diff] [blame] | 68 | xmlXPathFreeObject (result); |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 69 | } |
MDT 2003 John Fleck | bc6734a | 2003-08-28 15:01:40 +0000 | [diff] [blame] | 70 | xmlFreeDoc(doc); |
| 71 | xmlCleanupParser(); |
| 72 | return (1); |
| 73 | } |
MDT 2004 John Fleck | 4c3bb7d | 2004-08-25 02:51:27 +0000 | [diff] [blame] | 74 | ]]> |