用C++.NET创建XML文档(用正则表达式和字符串类两种方法匹配)

前一阵子搞java,可是SUN提供的帮助文档没有搜索功能,只提供了27个索引页,即26个字母开头和以‘_’开头的类或函数 ,所以想给它加个搜索功能,为此用C++.NET建了个XML文档用来查找,主要有两个关键字,是要搜索的类或函数,是包括链接信息的简要说明。如下图所示:

用C++.NET创建XML文档(用正则表达式和字符串类两种方法匹配)_第1张图片

源程序如下:



#using
#using
#using
#using
#using

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Windows::Forms;
using namespace System::Text::RegularExpressions;
int main(){
    String^ filename;
    String^ content;
    String^ key;
    String^ detail;
    String^ word1=L"(?<=

)([A]|[^A])*?(?=)";
    String^ word2=L"(?<=)([A]|[^A])*?(?=)";
     System::Text::Encoding^ temp=System::Text::Encoding::GetEncoding("gb2312");
   // XmlDocument^ temp=gcnew XmlDocument();
     array^btemp=gcnew array(word1->Length);
     btemp=temp->GetBytes(word1);
     Regex^ elementexp=gcnew Regex(temp->GetString(btemp));
     array^btemp1=gcnew array(word2->Length);
     btemp1=temp->GetBytes(word2);
     Regex^ keyexp=gcnew Regex(temp->GetString(btemp1));
    for(int i=1;i<=27;i++){
        //MessageBox::Show(L"OK",L"success",MessageBoxButtons::OK,MessageBoxIcon::Information);
        filename=String::Concat(L"D://Program Files//Apache Software Foundation//Tomcat 5.5//webapps//api//index-files//index-",i.ToString(),L".html");
     FileStream^ fileStream=gcnew FileStream(filename,FileMode::OpenOrCreate,FileAccess::Read);
                 array^b=gcnew array(fileStream->Length);
                 fileStream->Read(b,0,b->Length);
                 System::Text::Encoding^ gb=System::Text::Encoding::GetEncoding("gb2312");
                 content=gb->GetString(b);
                 XmlDocument^ document=gcnew XmlDocument();
                 XmlElement^ element;
                 XmlElement^ root;
                 XmlElement^ keyele;
                 XmlElement^ detailele;
                 XmlNode^ xmlNode;
                 XmlText^ keytext;
                 XmlText^ detailtext;
                 //Console::Write(content);
                 xmlNode=document->CreateNode(XmlNodeType::XmlDeclaration,L"",L"");
                 document->AppendChild(xmlNode);
                
                 root=document->CreateElement(L"",L"javaapi",L"");
                
                 Match^ eleMatch=nullptr;
                 Match^ keyMatch=nullptr;
                int index1=content->IndexOf(L"
");
                MatchCollection^ elematches=elementexp->Matches(content);
       for(int j=0;jCount;j++){
                /*while(index1Length){
            index1=content->IndexOf(L"
",index1+4);
            int index2=content->IndexOf(L"
",index1+4);
            int index5=index2;
            if(index2==-1)index2=content->IndexOf(L"
");
            detail=content->Substring(index1,index2-index1);
            int index3=detail->IndexOf(L"");
            int index4=detail->IndexOf(L"
");
            key=detail->Substring(index3+3,index4-index3-3);*/
            eleMatch=elematches[j];
             detail=eleMatch->ToString();
            keyMatch=keyexp->Matches(detail)[0];
            key=keyMatch->ToString();
            //MessageBox::Show(key,L"success",MessageBoxButtons::OK,MessageBoxIcon::Information);
            
             element=document->CreateElement(L"",L"element",L"");
             keyele=document->CreateElement(L"",L"key",L"");
             keytext=document->CreateTextNode(key);
             keyele->AppendChild(keytext);
             element->AppendChild(keyele);
           
             detailele=document->CreateElement(L"",L"detail",L"");
             detailtext=document->CreateTextNode(detail);
             detailele->AppendChild(detailtext);
             element->AppendChild(detailele);    
             root->AppendChild(element);
             //if(index5==-1)break;
             //Console::WriteLine(index1);
           }
     
       MessageBox::Show(L"OK",L"success",MessageBoxButtons::OK,MessageBoxIcon::Information);
                 document->AppendChild(root);
       String^ savename=String::Concat(L"java",i.ToString(),L".xml");
       XmlTextWriter^ writer=gcnew XmlTextWriter(savename,System::Text::Encoding::UTF8);
       document->WriteTo(writer);
       writer->Close();
            }   
    return 0;
        }

    绿色的代码使用正则表达式匹配,红色的代码使用String类匹配

    得到的XML文档如下所示:

用C++.NET创建XML文档(用正则表达式和字符串类两种方法匹配)_第2张图片

 

 

 

你可能感兴趣的:(.NET)