apd.html 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>D. Code for XPath Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Keyword Example"><link rel="next" href="ape.html" title="E. Code for Add Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for XPath Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="xpathappendix"></a>D. Code for XPath Example</h2></div></div><div></div></div><p>
  2. </p><pre class="programlisting">
  3. #include &lt;libxml/parser.h&gt;
  4. #include &lt;libxml/xpath.h&gt;
  5. xmlDocPtr
  6. getdoc (char *docname) {
  7. xmlDocPtr doc;
  8. doc = xmlParseFile(docname);
  9. if (doc == NULL ) {
  10. fprintf(stderr,"Document not parsed successfully. \n");
  11. return NULL;
  12. }
  13. return doc;
  14. }
  15. xmlXPathObjectPtr
  16. getnodeset (xmlDocPtr doc, xmlChar *xpath){
  17. xmlXPathContextPtr context;
  18. xmlXPathObjectPtr result;
  19. context = xmlXPathNewContext(doc);
  20. if (context == NULL) {
  21. printf("Error in xmlXPathNewContext\n");
  22. return NULL;
  23. }
  24. result = xmlXPathEvalExpression(xpath, context);
  25. xmlXPathFreeContext(context);
  26. if (result == NULL) {
  27. printf("Error in xmlXPathEvalExpression\n");
  28. return NULL;
  29. }
  30. if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
  31. xmlXPathFreeObject(result);
  32. printf("No result\n");
  33. return NULL;
  34. }
  35. return result;
  36. }
  37. int
  38. main(int argc, char **argv) {
  39. char *docname;
  40. xmlDocPtr doc;
  41. xmlChar *xpath = (xmlChar*) "//keyword";
  42. xmlNodeSetPtr nodeset;
  43. xmlXPathObjectPtr result;
  44. int i;
  45. xmlChar *keyword;
  46. if (argc &lt;= 1) {
  47. printf("Usage: %s docname\n", argv[0]);
  48. return(0);
  49. }
  50. docname = argv[1];
  51. doc = getdoc(docname);
  52. result = getnodeset (doc, xpath);
  53. if (result) {
  54. nodeset = result-&gt;nodesetval;
  55. for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
  56. keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
  57. printf("keyword: %s\n", keyword);
  58. xmlFree(keyword);
  59. }
  60. xmlXPathFreeObject (result);
  61. }
  62. xmlFreeDoc(doc);
  63. xmlCleanupParser();
  64. return (1);
  65. }
  66. </pre><p>
  67. </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C. Code for Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> E. Code for Add Keyword Example</td></tr></table></div></body></html>