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,还望大家多多指正。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值