p align="center"> 如何将一个超文本文件Html文件格式转换为Txt文本文件呢?这就是本文所要讨论的内容了。我们都知道,超文本文件有很多的标记,如$#@60;Html$#@62;、$#@60;Head$#@62;、$#@60;Body$#@62;等等,在文本文件中,这些都是没用的,我们要把它们都删去,而且超文本文件还有很多不可见的内容,如用“$#@60;!--”、“--$#@62;”括起来的内容、Java脚本程序,这些在文本文件中也是不可用的,我们也要把它们都删去。因此,我们可以得出这么一个结论: 一个简单的Html转换Txt程序的基本原理就是--将超文本文件不可视部分去掉,将超文本文件可视部分的内容写到文本文件中去。 好了,有了理论就应该有些实际的东西了。转换的步骤可以简单地归纳为:
1、 去掉$#@60;head$#@62;部分的内容
2、 去掉$#@60;script$#@62;部分的Java脚本程序
3、 转换$#@60;br为换行符
4、 转换$#@60;/p$#@62;为换行符
5、 转换和去掉其它所有的超文本标记
6、 转换“$#@60;”为“<”符号
7、 转换“$#@62;”为“>”符号
8、 转换“&”为“&”符号
9、 转换“ ”为空格符号
10、转换“"”为引号
11、去掉转换后开头和结尾出现的所有空格符号
12、转换完成
转换的步骤可用VB的函数IsStr来实现,如下面的代码可以去掉文本框Text3中超文本文件开头到$#@60;/Head$#@62;标记部分的内容:
Do While InStr(1, LCase(Text3.Text), "$#@60;/head$#@62;") <> 0
Text3.SelStart = 0 Text3.SelLength = InStr(1, LCase(Text3.Text), "$#@60;/head$#@62;") + 6 Text3.SelText = ""
Loop
详细的程序代码请参看程序清单或源程序。这个转换程序设计时考虑的是转换规范的超文本文件,当要转换的文件不够标准(如:有$#@60;/head$#@62;而没有$#@60;head$#@62;与之配对)的时候,转换就不能完成。而且,这个例子只转换了部分的超文本标记,还有许多的标记,如表单标记“FORM”并没有被转换,更多、更详尽的功能就有待你完成了。
附程序清单: Option Explicit Private Sub Form_Load()
CommonDialog1.CancelError = True
Text3.Visible = False Command1.Capti = "打开" Command2.Caption = "转换==$#@62;" Command3.Caption = "保存"
End Sub
Private Sub Command1_Click()
On Error Resume Next
Dim TextLine As String CommonDialog1.Filter = "网页|*.htm;*.html" CommonDialog1.ShowOpen If err $#@60;$#@62; 32755 Then
Text1 = ""
"打开文件 Open CommonDialog1.FileName For Input As #1 Do While Not EOF(1)
Line Input #1, TextLine
Text1 = Text1 & Trim(TextLine)
Loop
Close #1
Else
MsgBox "不能打开文件"
End If
End Sub
Private Sub Command3_Click()
On Error Resume Next
CommonDialog1.Filter = "文本文件|*.txt" CommonDialog1.ShowSave If err $#@60;$#@62; 32755 Then
Open CommonDialog1.FileName For Output As #1
Print #1, Text3 Close #1
Else
MsgBox "不能保存文件"
End If
End Sub
Private Sub Command2_Click()
Dim txtStr As String
On Error GoTo err Form1.MousePointer = 11 Text3.Text = Text1.Text DoEvents Form1.Caption = "正在去掉$#@60;head$#@62;部分..." "去掉$#@60;head$#@62;部分 Do While InStr(1, LCase(Text3.Text), "$#@60;/head$#@62;") $#@60;$#@62;
0
Text3.SelStart = 0
Text3.SelLength = InStr(1, LCase(Text3.Text), "$#@60;/head$#@62;")
+ 6 Text3.SelText = ""
Loop
Form1.Caption = "正在去掉$#@60;script$#@62;部分..." "去掉$#@60;script$#@62;部分 Do While InStr(1, LCase(Text3.Text), "$#@60;/script$#@62;") $#@60;$#@62;
0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@60;script")
- 1
Text3.SelLength = InStr(1, LCase(Text3.Text), "$#@60;/script$#@62;")
- Text3.SelStart + 9 Text3.SelText = ""
Loop
Form1.Caption = "正在转换$#@60;br$#@62;为换行符..." "转换$#@60;br$#@62;为换行符 Do While InStr(1, LCase(Text3.Text), "$#@60;br$#@62;") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@60;br$#@62;")
- 1
Text3.SelLength = 4 Text3.SelText = "" + vbCrLf
Loop
Form1.Caption = "正在转换$#@60;p$#@62;$#@60;/p$#@62;为换行符..." "转换$#@60;/p$#@62;为换行符 Do While InStr(1, LCase(Text3.Text), "$#@60;/p$#@62;") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@60;/p$#@62;")
- 1
Text3.SelLength = 4 Text3.SelText = "" + vbCrLf
Loop
Form1.Caption = "正在删除Html标记..." "去掉其它的Html标记 Do While InStr(1, LCase(Text3.Text), "$#@60;") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@60;")
- 1
Text3.SelLength = InStr(1, LCase(Text3.Text), "$#@62;") - Text3.SelStart Text3.SelText = ""
Loop
Form1.Caption = "正在转换"$#@60;"为"$#@60;"..." "转换"$#@60;"为"$#@60;" Do While InStr(1, LCase(Text3.Text), "$#@60;") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@60;")
- 1
Text3.SelLength = 4 Text3.SelText = "$#@60;"
Loop
Form1.Caption = "正在转换"$#@62;"为"$#@62;"..." "转换"$#@62;"为"$#@62;" Do While InStr(1, LCase(Text3.Text), "$#@62;") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "$#@62;")
- 1
Text3.SelLength = 4 Text3.SelText = "$#@62;"
Loop
Form1.Caption = "正在转换"&"为"&"..." "转换"&"为"&" Do While InStr(1, LCase(Text3.Text), "&") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), "&")
- 1
Text3.SelLength = 5 Text3.SelText = "&"
Loop
Form1.Caption = "正在转换" "为空格符..." "转换" "为" " Do While InStr(1, LCase(Text3.Text), " ") $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), " ")
- 1
Text3.SelLength = 6 Text3.SelText = " "
Loop
Form1.Caption = "正在转换"""为引号..." "转换"""为引号 Do While InStr(1, LCase(Text3.Text), """) $#@60;$#@62; 0
Text3.SelStart = InStr(1, LCase(Text3.Text), """)
- 1
Text3.SelLength = 6 Text3.SelText = """"
Loop
"去掉前后空格符 Text2.Text = Trim$(Text3.Text) Form1.Caption = "转换成功!" Form1.MousePointer = 0 Exit Sub
err:
Form1.Caption = "转换错误"
Form1.MousePointer = 0 MsgBox "转换时出错,请检查你的源文件!" Exit Sub
End Sub
|