Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | f9daq | 1 | /* |
| 2 | |||
| 3 | xpath2 ini/config.xml '//anode' |
||
| 4 | |||
| 5 | * section: XPath |
||
| 6 | * synopsis: Load a document, locate subelements with XPath, |
||
| 7 | * usage: xpath2 <xml-file> <xpath-expr> |
||
| 8 | * test: xpath2 test3.xml '//discarded' |
||
| 9 | */ |
||
| 10 | #include <stdlib.h> |
||
| 11 | #include <stdio.h> |
||
| 12 | #include <string.h> |
||
| 13 | #include <assert.h> |
||
| 14 | #include <vector> |
||
| 15 | |||
| 16 | #include <libxml/tree.h> |
||
| 17 | #include <libxml/parser.h> |
||
| 18 | #include <libxml/xpath.h> |
||
| 19 | #include <libxml/xpathInternals.h> |
||
| 20 | |||
| 21 | #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED) && \ |
||
| 22 | defined(LIBXML_OUTPUT_ENABLED) |
||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | static int getvalue(xmlXPathContextPtr xpathCtx, const xmlChar * xpathExpr, std::vector <xmlChar *> &retval); |
||
| 27 | static xmlChar * getvalue(xmlXPathContextPtr xpathCtx, const xmlChar * xpathExpr, int which=0); |
||
| 28 | |||
| 29 | |||
| 30 | int |
||
| 31 | main(int argc, char **argv) { |
||
| 32 | std::vector<xmlChar *> retval; |
||
| 33 | xmlDocPtr doc; |
||
| 34 | xmlXPathContextPtr xpathCtx; |
||
| 35 | int size; |
||
| 36 | |||
| 37 | xmlInitParser(); |
||
| 38 | LIBXML_TEST_VERSION |
||
| 39 | |||
| 40 | /* Load XML document */ |
||
| 41 | doc = xmlParseFile(argv[1]); |
||
| 42 | if (doc == NULL) { |
||
| 43 | fprintf(stderr, "Error: unable to parse file \"%s\"\n", argv[1]); |
||
| 44 | return(-1); |
||
| 45 | } |
||
| 46 | |||
| 47 | /* Create xpath evaluation context */ |
||
| 48 | xpathCtx = xmlXPathNewContext(doc); |
||
| 49 | if(xpathCtx == NULL) { |
||
| 50 | fprintf(stderr,"Error: unable to create new XPath context\n"); |
||
| 51 | xmlFreeDoc(doc); |
||
| 52 | return(-1); |
||
| 53 | } |
||
| 54 | |||
| 55 | for (int i=2; i<argc;i++){ |
||
| 56 | size = getvalue(xpathCtx, BAD_CAST argv[i] , retval); |
||
| 57 | printf ("-->size = %d value %s\n",size, getvalue(xpathCtx, BAD_CAST argv[i]) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | xmlXPathFreeContext(xpathCtx); |
||
| 61 | xmlFreeDoc(doc); |
||
| 62 | xmlCleanupParser(); |
||
| 63 | xmlMemoryDump(); |
||
| 64 | return 0; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * getvalue: |
||
| 70 | * @xpathCtx: the input xmlXPathContextPtr |
||
| 71 | * @xpathExpr: the xpath expression for evaluation. |
||
| 72 | * |
||
| 73 | * evaluates XPath expression |
||
| 74 | * |
||
| 75 | * Returns node content |
||
| 76 | */ |
||
| 77 | int getvalue(xmlXPathContextPtr xpathCtx, const xmlChar* xpathExpr, std::vector<xmlChar *> &retval) { |
||
| 78 | |||
| 79 | xmlXPathObjectPtr xpathObj; |
||
| 80 | xmlNodeSetPtr nodes; |
||
| 81 | xmlChar * ret=NULL; |
||
| 82 | int size; |
||
| 83 | int i; |
||
| 84 | assert(xpathExpr); |
||
| 85 | /* Evaluate xpath expression */ |
||
| 86 | xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); |
||
| 87 | if(xpathObj == NULL) { |
||
| 88 | fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); |
||
| 89 | return retval.size(); |
||
| 90 | } |
||
| 91 | |||
| 92 | nodes = xpathObj->nodesetval; |
||
| 93 | size = (nodes) ? nodes->nodeNr : 0; |
||
| 94 | for(i = 0; i< size; i++) { |
||
| 95 | ret = xmlNodeGetContent(nodes->nodeTab[i]); |
||
| 96 | if(ret) { |
||
| 97 | retval.push_back(ret); |
||
| 98 | printf("[%d] %s Return value: %s\n", i, xpathExpr, ret); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /* Cleanup of XPath data */ |
||
| 103 | xmlXPathFreeObject(xpathObj); |
||
| 104 | |||
| 105 | return retval.size(); |
||
| 106 | } |
||
| 107 | |||
| 108 | xmlChar * getvalue(xmlXPathContextPtr xpathCtx, const xmlChar* xpathExpr, int which) { |
||
| 109 | |||
| 110 | xmlXPathObjectPtr xpathObj; |
||
| 111 | xmlNodeSetPtr nodes; |
||
| 112 | xmlChar * ret=NULL; |
||
| 113 | int size; |
||
| 114 | int i; |
||
| 115 | assert(xpathExpr); |
||
| 116 | /* Evaluate xpath expression */ |
||
| 117 | xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); |
||
| 118 | if(xpathObj == NULL) { |
||
| 119 | fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); |
||
| 120 | return NULL; |
||
| 121 | } |
||
| 122 | |||
| 123 | nodes = xpathObj->nodesetval; |
||
| 124 | size = (nodes) ? nodes->nodeNr : 0; |
||
| 125 | for(i = 0; i< size; i++) { |
||
| 126 | ret = xmlNodeGetContent(nodes->nodeTab[i]); |
||
| 127 | if(ret) { |
||
| 128 | printf("%s Return value: %s\n", xpathExpr, ret); |
||
| 129 | return ret; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /* Cleanup of XPath data */ |
||
| 134 | xmlXPathFreeObject(xpathObj); |
||
| 135 | |||
| 136 | return NULL; |
||
| 137 | } |
||
| 138 | |||
| 139 | #endif |