我在这里粘贴一些代码,使用gcc file.c -lxml2编译没有警告,假设你的系统中安装了libxml2.
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <assert.h>
#include <libxml/tree.h>
#include <libxml/xpathInternals.h>
xmlDocPtr
getdoc (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) {
printf("Error in xmlXPathNewContext\n");
return NULL;
}
if(xmlXPathRegisterNs(context, BAD_CAST "new", BAD_CAST "http://www.example.com/new") != 0) {
fprintf(stderr,"Error: unable to register NS with prefix");
return …Run Code Online (Sandbox Code Playgroud)