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++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

C++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

C/C++的编译和链接过程

目录 从源文件生成可执行文件(书中第2章) 1.Preprocessing预处理——预处理器cpp 2.Compilation编译——编译器cll ps:vs中优化选项设置 3.Assembly汇编——汇编器as ps:vs中汇编输出文件设置 4.Linking链接——链接器ld 符号 模块,库 链接过程——链接器 链接过程 1.简单链接的例子 2.链接过程 3.地址和

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

vue, 左右布局宽,可拖动改变

1:建立一个draggableMixin.js  混入的方式使用 2:代码如下draggableMixin.js  export default {data() {return {leftWidth: 330,isDragging: false,startX: 0,startWidth: 0,};},methods: {startDragging(e) {this.isDragging = tr

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

React+TS前台项目实战(十七)-- 全局常用组件Dropdown封装

文章目录 前言Dropdown组件1. 功能分析2. 代码+详细注释3. 使用方式4. 效果展示 总结 前言 今天这篇主要讲全局Dropdown组件封装,可根据UI设计师要求自定义修改。 Dropdown组件 1. 功能分析 (1)通过position属性,可以控制下拉选项的位置 (2)通过传入width属性, 可以自定义下拉选项的宽度 (3)通过传入classN