如何将html文件批量转换成xml

作者:海涛 来源: 日期:2002-8-8
现在很多人时髦写xml、xhtml的网页,但现在fp2002和dw4都不支持。那么我怎么画呢?
那个xml的编辑工具xpy用起太不方便了。
-----------------------------------------
这个脚本是我自己修改的,可以做到95%了,自己再改一下就OK。
另外建议你把要转的html代码用dw打开套用样式保存一次,再用这个转换。一般绝大多数可以转好。


html2xsl.asp

<%
strtag_source1="<html ,<body ,<table ,<td ,<tr ,<option ,<font ,<div ,<span ,<h1 ,<h2 ,<form "
strtag1 = Split(strtag_source1, ",", -1, 1)

strattri_source2="selected,checked,norwap,readonly,noshade" '单独属性
strtag2 = Split(strattri_source2, ",", -1, 1)

strtag_source3="<input ,<img ,<hr ,<br ,<meta" '单端标签
strtag3 = Split(strtag_source3, ",", -1, 1)

'-------------------以下是处理flag值的多个函数
function func_flag1(tmp)'处理单个属性:(例:width=325)
index=InStr(1,tmp,"=",1)
z1=left(tmp,index)
z2=""""
z3=mid(tmp,index+1,len(tmp)-len(z1))
func_flag1=z1+z2+z3+z2
end function

function func_flag2(tmp)'(例:selected)
func_flag2=tmp+"="+""""+tmp+""""
end function

function func_flag3(tmp)'处理单端标签(例:<img…)
func_flag3=replace(cstr(tmp),">","/>")
end function


function alone_tag(tag)
dim tag1 '定义处理完以后的标签
tag=LCase(tag) '所有字符串都改写成小写形式
dim tmpattri '
index=InStr(1,tag," ",1)
tmpattri=right(tag,len(tag)-index) '除去左侧标签
if len(tmpattri)>1 then
tmpattri=trim(left(tmpattri,len(tmpattri)-1)) '除去右侧">",并去除两端空格(如果标签长大于等于2个字符)
end if

tmpattri=replace(tmpattri,chr(13)," ") '对源码中,一个标签不在一行里的情况,尚等待另行考虑处理。
tmpattri=replace(tmpattri,chr(10)," ")
tmpattri=replace(tmpattri,chr(10)&chr(13)," ")
tmpattri=replace(tmpattri," "," ") '【这两句是对付当属性串里有多个空格的时候,
tmpattri=replace(tmpattri," "," ") '【替换成一个空格,不过只能处理不超过16个空格的情况。
tmpattri=replace(tmpattri," "," ")
tmpattri=replace(tmpattri," "," ")
tmpattri=replace(tmpattri," "," ")
tmpattri=replace(tmpattri," "," ")
tmpattri=replace(tmpattri," "," ")
attribute=Split(tmpattri, " ", -1, 1) 

For count=0 to UBound(attribute, 1)
If InStr(1,attribute(count),"=",1)=0 Then
flag=2 '单个属性串中没找到等于号。(例:selected)
Else
IF InStr(1,attribute(count),"""",1)=0 Then
flag=1 '单个属性串中没找到等于号。(例:width=325)
Else
flag=0 '单个属性串找到了等于号。(例:width="325")
IF InStr(1,attribute(count),"""",1)>0 Then

End IF
End If
End If


Select case flag
case 0 attribute(count)=attribute(count)
case 1 attribute(count)=func_flag1(attribute(count)) '调用函数func_flag1处理。(例:width=325)
case 2 attribute(count)=func_flag2(attribute(count)) '调用函数func_flag2处理。(例:selected)
case 3 attribute(count)=func_flag3(attribute(count)) '调用函数func_flag3处理单端标签(例:<img…)
case 4 attribute(count)=(attribute(count)) '另行处理属性串之间包含分号、空格的情况
End Select
Next

count=0
for count=0 to UBound(attribute, 1)
attribute_tmp=attribute_tmp+" "+attribute(count) '属性值之间要有空格
next
index=InStr(1,tag," ",1)
if InStr(1,tag," ",1)=0 and len(tag)<>"" then 
tag1=Replace(tag,">"," >") 
else
tag_self=left(tag,index-1)
tag1=tag_self+attribute_tmp+">"
end if
for count=0 to UBound(strtag3,1)
if InStr(1,tag1,strtag3(count),1)<>0 then '这里利用到前面已切分好的属性标签
tag1=func_flag3(tag1) '对付单端标签——flag=3(例:<img…)
end if
next
alone_tag=tag1
end function







Function transform(txt)
dim alltmp '定义此字符串变量,只保留未处理的字符串部分。
alltmp=txt
alltmp=replace(alltmp," "," ") '是替换掉文本中的字符实体&#nbsp;
'处理htm源代码
do while trim(alltmp)<>""
'msgbox alltmp
index=0
index=InStr(1,alltmp,"<",1)
if index=1 then
index_right=InStr(1,alltmp,">",1)
tag=left(alltmp,index_right) 
if mid(tag,2,1)<>"/" then
tag1=alone_tag(tag)
txt1=txt1+tag1
del_tag=len(tag)
else 
txt1=txt1+LCase(tag)
del_tag=len(tag)
end if
else
if index>1 then
str_tmp=left(alltmp,index-1)
txt1=txt1+str_tmp
del_tag=len(left(alltmp,index-1))
end if
if index=0 then 
txt1=txt1+alltmp
del_tag=len(alltmp)
end if
end if

'response.write "alltmp="+alltmp
'减掉处理完的
alltmp=right(alltmp,len(alltmp)-del_tag) '(如果标签长大于等于2个字符)这里有问题!12.14,下次再作!!

loop

txt1=replace(txt1," ="""" "," ") '  =""
txt1=replace(txt1," >",">") ' >
txt1=replace(txt1,"<tbody>","") '<tbody>
transform=replace(txt1,"</tbody>","") '</tbody>

End Function



dim txt '源文本
dim txt1 '目标文本
dim tmpreadline '=thisfile.readline

txt="":txt1="":tmpReadAll=""
txt=request("intxt")

if txt="" then
%><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<form method="POST" action>
<p><textarea rows="10" name="intxt" cols="47"></textarea><br>
<input type="submit" value="提交" name="B1"></p>
</form>

<%
end if

txt1=transform(cstr(txt))

txt=replace(server.htmlencode(txt),chr(13)&chr(10),"<br>")+""

txt1=replace(server.htmlencode(txt1),chr(13)&chr(10),"<br>")+""

response.write txt1

%>
相关文章