本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0
#include <libxml/parser.h>                                                           
#include <libxml/xpath.h>                                                            
#include <stdio.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;                                                                     
 }                                                                                  
 result = xmlXPathEvalExpression(xpath, context);                                   
 xmlXPathFreeContext(context);                                                      
 if (result == NULL) {                                                              
  printf("Error in xmlXPathEvalExpression\n");                                     
  return NULL;                                                                     
 }                                                                                  
 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){                                    
  xmlXPathFreeObject(result);                                                      
    printf("No result\n");                                                           
  return NULL;                                                                     
 }                                                                                  
 return result;                                                                     
}                                                                                    
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
int main(int argc, char **argv) {                                                    
                                                                                     
 char *docname;                                                                     
 xmlDocPtr doc;                                                                     
 xmlChar *xpath = (xmlChar*) "//keyword";                                           
 xmlNodeSetPtr nodeset;                                                             
 xmlXPathObjectPtr result;                                                          
 int i;                                                                             
 xmlChar *keyword;                                                                  
                                                                                   
 if (argc <= 1) {                                                                   
  printf("Usage: %s docname\n", argv[0]);                                          
  return(0);                                                                       
 }                                                                                  
                                                                                     
 docname = argv[1];                                                                 
 doc = getdoc(docname);                                                             
 result = getnodeset (doc, xpath);                                                  
 if (result) {                                                                      
  nodeset = result->nodesetval;                                                    
  for (i=0; i < nodeset->nodeNr; i++) {                                            
   keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);  
    printf("keyword: %s\n", keyword);                                              
    xmlFree(keyword);                                                              
  }                                                                                
  xmlXPathFreeObject (result);                                                     
 }                                                                                  
 xmlFreeDoc(doc);                                                                   
 xmlCleanupParser();                                                                
 return (1);                                                                        
}                                                                                

demo.xml

 <?xml version="1.0" encoding="UTF-8"?>                 
<story>                                                
  <storyinfo>                                          
    <author>John Fleck</author>                        
    <datewritten>June 2, 2002</datewritten>            
    <keyword>example keyword裴</keyword>                
  </storyinfo>                                         
  <body>                                               
    <headline>This is the headline</headline>          
    <para>This is the body text.</para>                
  </body>                                              
</story>                                                
    
                                                                                     

恳请帮单击一下广告
posted on 2010-08-23 12:06 有猫相伴的日子 阅读(3189) 评论(2)  编辑  收藏 所属分类: c&c++

评论:
# re: libxml中使用xpath解析xml文件[未登录] 2011-04-13 17:27 | a
xmlChar *xpath = (xmlChar*) "//keyword"; 这里为什么要用"//"  回复  更多评论
  
# re: libxml中使用xpath解析xml文件[未登录] 2014-01-02 13:06 | 123
@a
你好好看一些啊xpath的语法  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网