VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/doc/tutorial/includexpath.c@ 7931

最後變更 在這個檔案從7931是 6076,由 vboxsync 提交於 17 年 前

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Date Revision Author Id
檔案大小: 1.5 KB
 
1<![CDATA[
2#include <libxml/parser.h>
3#include <libxml/xpath.h>
4
5xmlDocPtr
6getdoc (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
18xmlXPathObjectPtr
19getnodeset (xmlDocPtr doc, xmlChar *xpath){
20
21 xmlXPathContextPtr context;
22 xmlXPathObjectPtr result;
23
24 context = xmlXPathNewContext(doc);
25 if (context == NULL) {
26 printf("Error in xmlXPathNewContext\n");
27 return NULL;
28 }
29 result = xmlXPathEvalExpression(xpath, context);
30 xmlXPathFreeContext(context);
31 if (result == NULL) {
32 printf("Error in xmlXPathEvalExpression\n");
33 return NULL;
34 }
35 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
36 xmlXPathFreeObject(result);
37 printf("No result\n");
38 return NULL;
39 }
40 return result;
41}
42int
43main(int argc, char **argv) {
44
45 char *docname;
46 xmlDocPtr doc;
47 xmlChar *xpath = (xmlChar*) "//keyword";
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);
66 xmlFree(keyword);
67 }
68 xmlXPathFreeObject (result);
69 }
70 xmlFreeDoc(doc);
71 xmlCleanupParser();
72 return (1);
73}
74]]>
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette