c++编写html文件解析器

2024-03-02 17:58

本文主要是介绍c++编写html文件解析器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

      最近本来是要去那嵌入式课的,但是那课竟然说人数太少,开不了,靠。所以这两天只能自己研究点东西了。记得自己以前就想动手写一个关于dom的解析引擎,只怪自己太懒,一直没动笔。最近在家也没什么事做,就自己动手写一个,花了一天时间写的差不多了,正好锻炼自己的c++水平吧。

      这个解析程序有三个类,node,nodecollect,dom。dom这个类是个包装,它就相当与document吧,nodecollect是节点的集合,比如getelementsbytagname的返回集合吧,node类就是具体到节点。这个程序解析的原理就是解析一段html字符串,在里面提取节点,然后用链表存储节点,node类有两个私有对象,start,len。start就是该节点字符串到原始字符串的位置,len即使该节点字符串的长度。所以我们根据该节点的start和len就可以定位该节点在原始字符串的位置。

      好了,下面看代码:

#include<iostream>
using namespace std;
class node;
class dom;
class nodecollect{
private:
 node *n;
 int length;
public:
 nodecollect();
 ~nodecollect();
    int getlength();
 node* item(int i);
 void add(node *nn);
 
};
class node{
private:
 int start;
 int len;
public:
 char* innerhtml(dom& d);
 char* outerhtml(dom& d);
 char* innertext(dom& d);
 char* getattr(dom& d,char* str);
 char* tagname(dom& d);
 node* getparent(dom &d);
 nodecollect* getchild(dom &d);

node* getnext(dom &d);
 node* getprevious(dom &d);
 node *next;
 void setstart(int i);
 void setlen(int i);
 int getstart();
 int getlen();
};
class dom{
private:
 char *text;
 node *n;
 int count;
 int parse(char *s);
public:
 ~dom();
 char *gettext();
 void load(char *str);
 node* getitem(int i);
 int getcount();
 node *getbyid(char* id);
 nodecollect* getbytagname(char *tagname);
};
void dom::load(char* str){
 n=0;
 count=0;
 int l=strlen(str);
 text=new char[l+1];
 strcpy(text,str);
 char *t=text;
 parse(t);
 
}
int dom::getcount(){
 return count;
}
char *dom::gettext(){
 return text;
}
node* dom::getitem(int i){
 node* n1=n;
 while(i--){
  if(n1){
   n1=n1->next;
  }else{
   return 0;
  }
 }
 return n1;
 
}
node *dom::getbyid(char *id){
 for(int i=0;i<this->getcount();i++){
  if(::stricmp(this->getitem(i)->getattr(*this,"id"),id)==0){
   return this->getitem(i);
  }
 }
 return 0;
}
nodecollect* dom::getbytagname(char *tagname){
 nodecollect *nnode=new nodecollect;
 
 for(int i=0;i<this->getcount();i++){
 // cout<<strlen(this->getitem(i)->tagname(*this))<<endl;
  if(::stricmp(this->getitem(i)->tagname(*this),tagname)==0){
   nnode->add(this->getitem(i));
  }
 }
 return nnode;
}
dom::~dom(){
 delete[] text;
 node *n1=n,*n2;
 if(n1){
  while(n1->next!=0){
   n2=n1;
   n1=n1->next;
   delete n2;
  }
 }
}
int dom::parse(char *s){
 int i1=0,i2=0,i3=0;
 while(*s!=0){
  
  if(*s==0){
   return (long)s;
  }
  if(i3==1){
  if(*s=='/"'){
   if(i1==0){
    i1=1;
   }else{
    i1=0;
   }
  }
  if(*s=='/''){
   if(i2==0){
    i2=1;
   }else{
    i2=0;
   }
  }
  }
  if(*s=='<' && *(s+1)=='!'){
   
   if(i1==0 && i2==0){
    i3=1;
    node *nn=new node;
    nn->setstart(s-text);
    nn->setlen(0);
    nn->next=0;
    
    if(n){
     node *n1=n;
     while(n1->next!=0){
      n1=n1->next;
     }
     n1->next=nn;
    }else{
     n=nn;
    }
    int s1=(long)s;
    while(*s){
     if(*s=='/"'){
      if(i1==0){
       i1=1;
      }else{
       i1=0;
      }
     }
     if(*s=='/''){
      if(i2==0){
       i2=1;
      }else{
       i2=0;
      }
     }
     if(*s=='>'){
      if(i1==0 && i2==0){
       //cout<<(long)s+1-s1<<endl;
       nn->setlen((long)s+1-s1);
       s++;
       break;
      }
      
     }
     s++;
    }
    count++;
   }
  }
  if(*s=='<' && *(s+1)!='/' && *(s+1)!='!'){
   
   if(i1==0 && i2==0){
    i3=1;
    node *nn=new node;
    //cout<<s-text<<endl;
    nn->setstart(s-text);
    nn->setlen(0);
    nn->next=0;
    
    if(n){
     node *n1=n;
     while(n1->next!=0){
      n1=n1->next;
     }
     n1->next=nn;
    }else{
     n=nn;
    }
      count++;
   }
  }
  if(*s=='>'){
   if(i1==0 && i2==0){
    i3=0;
   }
  }
  if(*s=='/' && *(s+1)=='>'){
   if(i1==0 && i2==0){
    i3=0;
    node *n1=n;
    while(n1->next!=0){
     n1=n1->next;
    }
    // cout<<(long)s+2-(n1->getstart())-(long)text<<endl;
    n1->setlen((long)s+2-(n1->getstart())-(long)text);
   }
  }
  
  if(*s=='<' && *(s+1)=='/'){
   if(i1==0 && i2==0){
    i3=0;
    node *n1=n;
    node* min;
    int i=0;
    while(n1!=0){
     if(n1->getlen()==0){
      min=n1;
      // cout<<min->getstart()<<"*"<<i<<endl;
     }
     n1=n1->next;
     i++;
    }
    n1=min;
    // cout<<n1->getstart()<<endl;
    // cout<<(long)s-(long)text-n1->getstart()<<endl;
    while(*s!='>'){
     s++;
    }
    n1->setlen((long)s+1-(n1->getstart())-(long)text);
  
   }
  }
  
  s++;
 }
 
}
void node::setstart(int i){
 start=i;
}
void node::setlen(int i){
 len=i;
}
int node::getstart(){
 return start;
}
int node::getlen(){
 return len;
}
char* node::getattr(dom& d,char *str){
 char *out=outerhtml(d);
 int i1=0,i2=0;
 char *v=new char[strlen(out)+1];
 ::memset(v,0,strlen(out)+1);
 while(*out!=0){
  
  if(*out==0){
   return v;
  }
  if(*out=='/"'){
   if(i1==0){
    i1=1;
   }else{
    i1=0;
   }
  }
  if(*out=='/''){
   if(i2==0){
    i2=1;
   }else{
    i2=0;
   }
  }
  if(*out=='>'){
   if(i1==0 && i2==0){
    return v;
   }
  }
  char *s=strstr(out,str);
  if(s!=0 && s-out==0){
   if(i1==0 && i2==0){
    if(*(s-1)==' ' && *(s+strlen(str))=='=' && (*(s+strlen(str)+1)=='/'' || *(s+strlen(str)+1)=='/"')){
     char f=*(s+strlen(str)+1);
     char *t=s+strlen(str)+2;
     int ii=0;
     while(*t!=f || *(t-1)=='//'){
      v[ii]=*t;
      t++;
      ii++;
     }
     return v;
    }
   }
   
  }
  out++;
 }
 
}
node *node::getparent(dom& d){
int p=-1;
for(int i=0;i<d.getcount();i++){
 if(d.getitem(i)->getstart()<start){
  if(d.getitem(i)->getlen()+d.getitem(i)->getstart()>start+len){
   p=i;
  }
 }else{
  break;
 }
}
if(p==-1){
 return 0;
}else{
 return d.getitem(p);
}
}
nodecollect* node::getchild(dom &d){
int p=-1;
nodecollect *nn=new nodecollect;
for(int i=0;i<d.getcount();i++){
 if(d.getitem(i)->getstart()>start){
  p=i;
  break;
 }
}
if(p!=-1){
 for(;p<d.getcount();p++){
  if(start+len>d.getitem(p)->getlen()+d.getitem(p)->getstart()){
   nn->add(d.getitem(p));
  }else{
   break;
  }
 }

}
return nn;
}
char *node::outerhtml(dom &d){
 char *out=new char[len+1];
 char *c=d.gettext()+start;
 for(int i=0;i<len;i++){
  *(out+i)=*(c+i);
 }
 out[len]=0;
 return out;
}
char* node::tagname(dom &d){
char *out=this->outerhtml(d);
char *tag=new char[strlen(out)+1];
int i=1;
for(;*(out+i)!=' ' && *(out+i)!='-' && *(out+i)!='/' && *(out+i)!='>';i++){
tag[i-1]=*(out+i);
}
tag[i-1]=0;
return tag;
}
char *node::innerhtml(dom& d){
char* out=outerhtml(d);
char *base=out;
int l=strlen(out);
int i1=0,i2=0;
 char *inner=new char[strlen(out)+1];
 ::memset(inner,0,strlen(out)+1);
 while(*out!=0){
  
  if(*out==0){
   return inner;
  }
  if(*out=='/"'){
   if(i1==0){
    i1=1;
   }else{
    i1=0;
   }
  }
  if(*out=='/''){
   if(i2==0){
    i2=1;
   }else{
    i2=0;
   }
  }
  if(*out=='>'){
   if(i1==0 && i2==0){
    break;
   }
  }
  out++;
 }
 int innerlen=l-(strlen(tagname(d))+3)-(out-base+1);
 if(innerlen==0){
  return inner;
 }else{
 for(int i=0;i<innerlen;i++){
  inner[i]=*(out+i+1);
 }
 return inner;
 }
}
char *node::innertext(dom& d){
 char *h=innerhtml(d);
 char *inner;
 if(h[0]==0){
  inner=new char;
  *inner=0;
  return inner;
 }else{
  inner=new char[strlen(h)+1];
  ::memset(inner,0,strlen(h)+1);
 }
 int i=0,i1=0,i2=0,i3=0;
 for(;*h!=0;h++){
  
  if(*h==0){
   return inner;
  }
  if(*h=='<'){
   if(i3==0){
    i3=1;
    
   }
  }
  if(i3==1){
   if(*h=='/"'){
    if(i1==0){
     i1=1;
    }else{
     i1=0;
    }
    
   }
   if(*h=='/''){
    if(i2==0){
     i2=1;
    }else{
     i2=0;
    }
    
   }
   if(*h=='>'){
    if(i1==0 && i2==0){
     i3=0;
     
    }
   }
  }else{
   //cout<<*h;
   *(inner+i)=*h;
   i++;
  }
  
  
 }
 return inner;
}

node* node::getprevious(dom &d){
 node *nn=0;
 for(int i=0;i<d.getcount();i++){

  if(d.getitem(i)->getstart()==start && d.getitem(i)->getlen()==len){
   break;
  }else{
   if(start>=d.getitem(i)->getstart()+d.getitem(i)->getlen()){
    nn=d.getitem(i);
    
   }
  }
 }
 return nn;
}
node* node::getnext(dom& d){
 node *nn=0;
 for(int i=0;i<d.getcount();i++){
   if(start+len<=d.getitem(i)->getstart()){
    nn=d.getitem(i);
    break;
   }
 }
 return nn;
}
nodecollect::nodecollect(){
 n=0;
 length=0;
}
int nodecollect::getlength(){
 return length;
}
node *nodecollect::item(int i){
node* n1=n;
 while(i--){
  if(n1){
   n1=n1->next;
  }else{
   return 0;
  }
 }
 return n1;
}
void nodecollect::add(node *nn){
 node *n1=new node;
 n1->setstart(nn->getstart());
 n1->setlen(nn->getlen());
 n1->next=0;
 if(n){
  node *n2=n;
  while(n2->next){
   n2=n2->next;
  }
  n2->next=n1;
  
 }else{
  n=n1;
  
 }
 length++;
}
nodecollect::~nodecollect(){
 node *n1=n,*n2;
 if(n1){
  while(n1->next!=0){
   n2=n1;
   n1=n1->next;
   delete n2;
  }
 }
}

不好意思,代码有点长,大概500多行吧,不过也没办法,因为在解析时要考虑到的情况太多了,所以就显得有点冗长。下面我列出每个类的接口:

class nodecollect{
private:
 node *n;
 int length;
public:
 nodecollect();
 ~nodecollect();
    int getlength();
 node* item(int i);
 void add(node *nn);
 
};
class node{
private:
 int start;
 int len;
public:
 char* innerhtml(dom& d);
 char* outerhtml(dom& d);
 char* innertext(dom& d);
 char* getattr(dom& d,char* str);
 char* tagname(dom& d);
 node* getparent(dom &d);
 nodecollect* getchild(dom &d);

node* getnext(dom &d);
 node* getprevious(dom &d);
 node *next;
 void setstart(int i);
 void setlen(int i);
 int getstart();
 int getlen();
};
class dom{
private:
 char *text;
 node *n;
 int count;
 int parse(char *s);
public:
 ~dom();
 char *gettext();
 void load(char *str);
 node* getitem(int i);
 int getcount();
 node *getbyid(char* id);
 nodecollect* getbytagname(char *tagname);
};

大家可以看到和标准的dom操作还是很类似的吧。node类的许多方法都有个dom类型的参数,因为我们要对dom对象里的node链表进行遍历,查找到符合要求的项。

      html解析的核心代码看下面:

int dom::parse(char *s){
 int i1=0,i2=0,i3=0;
 while(*s!=0){
  
  if(*s==0){
   return (long)s;
  }
  if(i3==1){
  if(*s=='/"'){
   if(i1==0){
    i1=1;
   }else{
    i1=0;
   }
  }
  if(*s=='/''){
   if(i2==0){
    i2=1;
   }else{
    i2=0;
   }
  }
  }
  if(*s=='<' && *(s+1)=='!'){
   
   if(i1==0 && i2==0){
    i3=1;
    node *nn=new node;
    nn->setstart(s-text);
    nn->setlen(0);
    nn->next=0;
    
    if(n){
     node *n1=n;
     while(n1->next!=0){
      n1=n1->next;
     }
     n1->next=nn;
    }else{
     n=nn;
    }
    int s1=(long)s;
    while(*s){
     if(*s=='/"'){
      if(i1==0){
       i1=1;
      }else{
       i1=0;
      }
     }
     if(*s=='/''){
      if(i2==0){
       i2=1;
      }else{
       i2=0;
      }
     }
     if(*s=='>'){
      if(i1==0 && i2==0){
       //cout<<(long)s+1-s1<<endl;
       nn->setlen((long)s+1-s1);
       s++;
       break;
      }
      
     }
     s++;
    }
    count++;
   }
  }
  if(*s=='<' && *(s+1)!='/' && *(s+1)!='!'){
   
   if(i1==0 && i2==0){
    i3=1;
    node *nn=new node;
    //cout<<s-text<<endl;
    nn->setstart(s-text);
    nn->setlen(0);
    nn->next=0;
    
    if(n){
     node *n1=n;
     while(n1->next!=0){
      n1=n1->next;
     }
     n1->next=nn;
    }else{
     n=nn;
    }
      count++;
   }
  }
  if(*s=='>'){
   if(i1==0 && i2==0){
    i3=0;
   }
  }
  if(*s=='/' && *(s+1)=='>'){
   if(i1==0 && i2==0){
    i3=0;
    node *n1=n;
    while(n1->next!=0){
     n1=n1->next;
    }
    // cout<<(long)s+2-(n1->getstart())-(long)text<<endl;
    n1->setlen((long)s+2-(n1->getstart())-(long)text);
   }
  }
  
  if(*s=='<' && *(s+1)=='/'){
   if(i1==0 && i2==0){
    i3=0;
    node *n1=n;
    node* min;
    int i=0;
    while(n1!=0){
     if(n1->getlen()==0){
      min=n1;
      // cout<<min->getstart()<<"*"<<i<<endl;
     }
     n1=n1->next;
     i++;
    }
    n1=min;
    // cout<<n1->getstart()<<endl;
    // cout<<(long)s-(long)text-n1->getstart()<<endl;
    while(*s!='>'){
     s++;
    }
    n1->setlen((long)s+1-(n1->getstart())-(long)text);
  
   }
  }
  
  s++;
 }
 
}

我本来想用递归的,但是想了一会没什么思路,就用迭代了,上面代码对每一个字符进行扫描,对html里特征字符进行记录,来提取节点的。

下面看个例子:

main(){
 dom d;
 d.load("<html><body>sadas<a/><a hre='dsf'>as'</a></body></html>");
//for(int i=0;i<d.getcount();i++)
// cout<<d.getitem(i)->getstart()<<"    "<<d.getitem(i)->getlen()<<endl;
 cout<<d.getbytagname("a")->item(1)->innertext(d);
}

什么getparent就是获得父节点,返回node对象指针,getchild就是获得子节点集合,返回nodecollect对象指针。其他的顾名思义了。注意在返回节点指针时,你要先判断其指针是否为0,为0即代表该节点不存在。

       该程序进行了许多测试,但肯定还有许多bug,还望大家多多指正。

这篇关于c++编写html文件解析器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766959

相关文章

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基