博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
libxml中使用xpath解析xml文件
阅读量:3588 次
发布时间:2019-05-20

本文共 1685 字,大约阅读时间需要 5 分钟。

#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>                                               

转载地址:http://zlvwn.baihongyu.com/

你可能感兴趣的文章
eclipse常用快捷键
查看>>
html大作业笔记
查看>>
力扣 3. 无重复字符的最长子串
查看>>
力扣617. 合并二叉树
查看>>
力扣461. 汉明距离
查看>>
力扣 7. 整数反转
查看>>
力扣46. 全排列
查看>>
力扣22. 括号生成
查看>>
linux报错:export `xxxxx' not a valid identifier的一般原因
查看>>
linux 编译Java文件 报错 could not create parent directories
查看>>
[解决]VSCode在Linux下导入c语言头文件警告
查看>>
VS Code 编辑文件延迟大,卡顿的解决办法
查看>>
Java中递归的如果想要达到C++的传参效果的一种写法
查看>>
Hadoop在window上运行出现:java.io.IOException: (null) entry in command string: null chmod 0644
查看>>
最小生成树的 Krusal 算法和 Prim 算法 Java 实现
查看>>
CentOS下设置默认JDK
查看>>
剑指offer 43.左旋转字符串
查看>>
剑指offer 47. 求1 + 2 + 3 + .... + n
查看>>
分布式和集群的区别
查看>>
本科毕设完整流程和注意事项
查看>>