read_xml

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

int parseStory(xmlDocPtr doc, xmlNodePtr cur)
{
  xmlChar *key;
  cur = cur->xmlChildrenNode;
  while(cur != NULL)
  {
    //if((!xmlStrcmp(cur->name, (const xmlChar*)"abc")))
    //{
    //  printf("[%s]\n", cur->name);
    //  parseStory(doc, cur);
    //}
    
    if((!xmlStrcmp(cur->name, (const xmlChar*)"mysql")))
    {
      printf("[%s]\n", cur->name);
      parseStory(doc, cur);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"host")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"port")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"db")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"username")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"password")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"credits")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    else if((!xmlStrcmp(cur->name, (const xmlChar*)"instructor")))
    {
      key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf(" %s=>%s\n", cur->name, key);
      xmlFree(key);
    }
    
    
    
    
    cur = cur->next;
  }
  
  return 0;
  
}

int main(int argc, char **argv)
{
  //定义2个指针,doc指向整个dom;cur指向结点,以后遍历树就靠这个指针了
  xmlDocPtr doc;
  xmlNodePtr cur;
  
  //获取doc指针,也是把其他格式转成utf8的功能吧
  doc=xmlParseFile(argv[1]);
  if(doc==NULL)
  {
    printf("Document not parsed successfully. \n");
    exit(1);
  }
  
  printf("xmlParseFile ok.\n");
  
  // 取得结点指针
  cur=xmlDocGetRootElement(doc);
  if(cur==NULL)
  {
    printf("empty document. \n");
    xmlFreeDoc(doc);
    exit(1);
  }
  printf("xmlDocGetRootElement ok.\n");
  
  // 取得根结点指针,我这里是root,记住,这里一定要是根结点
  if (xmlStrcmp(cur->name, (const xmlChar *)"root"))
  {
    printf("document of the wrong type, root node != root\n");
    xmlFreeDoc(doc);
    exit(1);
  }
  printf("ok.\n");
  
  //通过这个递归函数,遍历出所有感兴趣的结点。
  parseStory(doc, cur);
  
  //一定要释放doc哦。
  xmlFreeDoc(doc);
  exit(0);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值