以下内容为程序代码:
<HTML> <HEAD> <TITLE>网易分类广告</TITLE> <META HTTP-EQUIV=Content-Type CONTENT="text/html; charset=gb2312"> <link rel=stylesheet href="http://classad.163.com/html/style.css"> </HEAD> <BODY topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=ffffff> <table width=100% cellspacing=0 cellpadding=10 border=0> <tr align=center> <td class=p7><b>如何将html批量转换成xml</b><BR><font class=grey>发布时间:2003-11-04</font></td> </tr> <tr> <td class=p4> <table border=0 cellspacing=0 cellpadding=0 align=right> <tr><td></td></tr> </table> 现在很多人时髦写xml、xhtml的网页,但现在fp2002和dw4都不支持。那么我怎么画呢?<br /> 那个xml的编辑工具xpy用起太不方便了。这个脚本是我自己修改的,可以做到95%了,自己再改一下就OK。<br /> 另外建议你把要转的html代码用dw打开套用样式保存一次,再用这个转换。一般绝大多数可以转好。 <br /> <br /> <br /> html2xsl.asp<br /> <br /> <%<br /> strtag_source1="<html ,<body ,<table ,<td ,<tr ,<option ,<font ,<div ,<span ,<h1 ,<h2 ,<form "<br /> strtag1 = Split(strtag_source1, ",", -1, 1)<br /> <br /> strattri_source2="selected,checked,norwap,readonly,noshade" '单独属性<br /> strtag2 = Split(strattri_source2, ",", -1, 1)<br /> <br /> strtag_source3="<input ,<img ,<hr ,<br ,<meta" '单端标签<br /> strtag3 = Split(strtag_source3, ",", -1, 1)<br /> <br /> '-------------------以下是处理flag值的多个函数<br /> function func_flag1(tmp)'处理单个属性:(例:width=325)<br /> index=InStr(1,tmp,"=",1)<br /> z1=left(tmp,index)<br /> z2=""""<br /> z3=mid(tmp,index+1,len(tmp)-len(z1))<br /> func_flag1=z1+z2+z3+z2<br /> end function<br /> <br /> function func_flag2(tmp)'(例:selected)<br /> func_flag2=tmp+"="+""""+tmp+""""<br /> end function<br /> <br /> function func_flag3(tmp)'处理单端标签(例:<img…)<br /> func_flag3=replace(cstr(tmp),">","/>")<br /> end function<br /> <br /> <br /> function alone_tag(tag)<br /> dim tag1 '定义处理完以后的标签<br /> tag=LCase(tag) '所有字符串都改写成小写形式<br /> dim tmpattri '<br /> index=InStr(1,tag," ",1)<br /> tmpattri=right(tag,len(tag)-index) '除去左侧标签<br /> if len(tmpattri)>1 then<br /> tmpattri=trim(left(tmpattri,len(tmpattri)-1)) '除去右侧">",并去除两端空格(如果标签长大于等于2个字符)<br /> end if<br /> <br /> tmpattri=replace(tmpattri,chr(13)," ") '对源码中,一个标签不在一行里的情况,尚等待另行考虑处理。<br /> tmpattri=replace(tmpattri,chr(10)," ")<br /> tmpattri=replace(tmpattri,chr(10)&chr(13)," ")<br /> tmpattri=replace(tmpattri," "," ") '【这两句是对付当属性串里有多个空格的时候,<br /> tmpattri=replace(tmpattri," "," ") '【替换成一个空格,不过只能处理不超过16个空格的情况。<br /> tmpattri=replace(tmpattri," "," ")<br /> tmpattri=replace(tmpattri," "," ")<br /> tmpattri=replace(tmpattri," "," ")<br /> tmpattri=replace(tmpattri," "," ")<br /> tmpattri=replace(tmpattri," "," ")<br /> attribute=Split(tmpattri, " ", -1, 1) <br /> <br /> For count=0 to UBound(attribute, 1)<br /> If InStr(1,attribute(count),"=",1)=0 Then<br /> flag=2 '单个属性串中没找到等于号。(例:selected)<br /> Else<br /> IF InStr(1,attribute(count),"""",1)=0 Then<br /> flag=1 '单个属性串中没找到等于号。(例:width=325)<br /> Else<br /> flag=0 '单个属性串找到了等于号。(例:width="325")<br /> IF InStr(1,attribute(count),"""",1)>0 Then<br /> <br /> End IF<br /> End If<br /> End If<br /> <br /> <br /> Select case flag<br /> case 0 attribute(count)=attribute(count)<br /> case 1 attribute(count)=func_flag1(attribute(count)) '调用函数func_flag1处理。(例:width=325)<br /> case 2 attribute(count)=func_flag2(attribute(count)) '调用函数func_flag2处理。(例:selected)<br /> case 3 attribute(count)=func_flag3(attribute(count)) '调用函数func_flag3处理单端标签(例:<img…)<br /> case 4 attribute(count)=(attribute(count)) '另行处理属性串之间包含分号、空格的情况<br /> End Select<br /> Next<br /> <br /> count=0<br /> for count=0 to UBound(attribute, 1)<br /> attribute_tmp=attribute_tmp+" "+attribute(count) '属性值之间要有空格<br /> next<br /> index=InStr(1,tag," ",1)<br /> if InStr(1,tag," ",1)=0 and len(tag)<>"" then <br /> tag1=Replace(tag,">"," >") <br /> else<br /> tag_self=left(tag,index-1)<br /> tag1=tag_self+attribute_tmp+">"<br /> end if<br /> for count=0 to UBound(strtag3,1)<br /> if InStr(1,tag1,strtag3(count),1)<>0 then '这里利用到前面已切分好的属性标签<br /> tag1=func_flag3(tag1) '对付单端标签——flag=3(例:<img…)<br /> end if<br /> next<br /> alone_tag=tag1<br /> end function<br /> <br /> <br /> <br /> <br /> Function transform(txt)<br /> dim alltmp '定义此字符串变量,只保留未处理的字符串部分。<br /> alltmp=txt<br /> alltmp=replace(alltmp," "," ") '是替换掉文本中的字符实体nbsp;<br /> '处理htm源代码<br /> do while trim(alltmp)<>""<br /> 'msgbox alltmp<br /> index=0<br /> index=InStr(1,alltmp,"<",1)<br /> if index=1 then<br /> index_right=InStr(1,alltmp,">",1)<br /> tag=left(alltmp,index_right) <br /> if mid(tag,2,1)<>"/" then<br /> tag1=alone_tag(tag)<br /> txt1=txt1+tag1<br /> del_tag=len(tag)<br /> else <br /> txt1=txt1+LCase(tag)<br /> del_tag=len(tag)<br /> end if<br /> else<br /> if index>1 then<br /> str_tmp=left(alltmp,index-1)<br /> txt1=txt1+str_tmp<br /> del_tag=len(left(alltmp,index-1))<br /> end if<br /> if index=0 then <br /> txt1=txt1+alltmp<br /> del_tag=len(alltmp)<br /> end if<br /> end if<br /> <br /> 'response.write "alltmp="+alltmp<br /> '减掉处理完的<br /> alltmp=right(alltmp,len(alltmp)-del_tag) '(如果标签长大于等于2个字符)这里有问题!12.14,下次再作!!<br /> <br /> loop<br /> <br /> txt1=replace(txt1," ="""" "," ") ' =""<br /> txt1=replace(txt1," >",">") ' ><br /> txt1=replace(txt1,"<tbody>","") '<tbody><br /> transform=replace(txt1,"</tbody>","") '</tbody><br /> <br /> End Function<br /> <br /> <br /> dim txt '源文本<br /> dim txt1 '目标文本<br /> dim tmpreadline '=thisfile.readline<br /> <br /> txt="":txt1="":tmpReadAll=""<br /> txt=request("intxt")<br /> <br /> if txt="" then<br /> %><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br /> <form method="POST" action><br /> <p><textarea rows="10" name="intxt" cols="47"></textarea><br><br /> <input type="submit" value="提交" name="B1"></p><br /> </form><br /> <br /> <%<br /> end if<br /> <br /> txt1=transform(cstr(txt))<br /> <br /> txt=replace(server.htmlencode(txt),chr(13)&chr(10),"<br>")+""<br /> <br /> txt1=replace(server.htmlencode(txt1),chr(13)&chr(10),"<br>")+""<br /> <br /> response.write txt1<br /> <br /> %> <br /> <br /> <br /> </td> </tr> </table> </BODY> </HTML> |