0

假设我有一个具有该结构的 HTML 文件

<html>
<head>
</head>
<body>
<div class="nav mainnavs"></div>
<div class="nav askquestion"></div>
</body>
</html>

我想插入这个文本

 <ul>
                    <li><a id="nav-questions" href="/questions">Questions</a></li>
                    <li><a id="nav-tags" href="/tags">Tags</a></li>
                    <li><a id="nav-users" href="/users">Users</a></li>

                    <li><a id="nav-badges" href="/badges">Badges</a></li>
                    <li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
                </ul>

使用 vb6在该文件中的两个层之间。

结果应该是这样的:

  <html>
<head>
</head>
<body>
<div class="nav mainnavs"></div>
 <ul>
                    <li><a id="nav-questions" href="/questions">Questions</a></li>
                    <li><a id="nav-tags" href="/tags">Tags</a></li>
                    <li><a id="nav-users" href="/users">Users</a></li>

                    <li><a id="nav-badges" href="/badges">Badges</a></li>
                    <li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
                </ul>
<div class="nav askquestion"></div>
</body>
</html>
4

3 回答 3

3

快速而肮脏的方法是进行一些字符串操作

Dim myHtmlFile As String
    Dim topPart As String
    Dim topSearch As String
    Dim bottomPart As String
    Dim bottomSearch As String
    Dim newPart As String

    'Load contents of file into string
    'You would do some I/O stuff here to get the file in a variable
    'Examples are pretty easy to find.  Google "vba read file into variable"
    myHtmlFile = "<html> " & vbCrLf & _
                "<head>" & vbCrLf & _
                "</head>" & vbCrLf & _
                "<body>" & vbCrLf & _
                "<div class=""nav mainnavs""></div>" & vbCrLf & _
                "<div class=""nav askquestion""></div>" & vbCrLf & _
                "</body>" & vbCrLf & _
                "</html>"

    topSearch = "<div class=""nav mainnavs""></div>" & vbCrLf
    bottomSearch = "<div class=""nav askquestion""></div>" & vbCrLf

    topPart = Left$(myHtmlFile, InStr(1, myHtmlFile, topSearch) + Len(topSearch) - 1)
    bottomPart = Mid$(myHtmlFile, InStrRev(myHtmlFile, bottomSearch))

    newPart = "<ul> " & vbCrLf & _
                    "<li><a id=""nav-questions"" href=""/questions"">Questions</a></li> " & vbCrLf & _
                    "<li><a id=""nav-tags"" href=""/tags"">Tags</a></li> " & vbCrLf & _
                    "<li><a id=""nav-users"" href=""/users"">Users</a></li> " & vbCrLf & _
                    "<li><a id=""nav-badges"" href=""/badges"">Badges</a></li> " & vbCrLf & _
                    "<li><a id=""nav-unanswered"" href=""/unanswered"">Unanswered</a></li> " & vbCrLf & _
                "</ul> "


    myHtmlFile = topPart & newPart & bottomPart

    'Now write the file back out 
于 2012-08-27T13:56:01.253 回答
0

我会尝试正则表达式。

起点: 如何在 Microsoft Visual Basic 6.0 中使用正则表达式

我不知道你对正则表达式有多熟悉。但这是我对如何做你想做的事情的想法:你必须创建一个与该行匹配的表达式,<div class="nav mainnavs"></div>然后调用方法 match,获取匹配的字符串索引并在那里插入你创建的<ul>...</ul>字符串。

于 2012-08-27T13:32:21.543 回答
0
    Dim sFileText As String
    Dim iFileNo As Integer
      iFileNo = FreeFile
          'open the file for writing
    Open App.Path & "\" & "test.html" For Output As #iFileNo

    'please note, if this file already exists it will be overwritten!

          'write some example html to the file



Print #iFileNo, "<html>"
Print #iFileNo, "<head>"
Print #iFileNo, "</head>"
Print #iFileNo, "<body>"
Print #iFileNo, " "
Print #iFileNo, "<div class="&"nav mainnavs"&"></div>"
Print #iFileNo, " <ul>"
Print #iFileNo, "                    <li><a id="&"nav-questions" href="&"/questions"&">Questions</a></li>"
Print #iFileNo, "                    <li><a id="&"nav-tags"&" href="&"/tags"&">Tags</a></li>"
Print #iFileNo, "                    <li><a id="&"nav-users"&" href="&"/users"&">Users</a></li>"
Print #iFileNo, " "
Print #iFileNo, "                    <li><a id="&"nav-badges"&" href="&"/badges"&">Badges</a></li>"
Print #iFileNo, "         <li><a id="&"nav-unanswered"&" href="&"/unanswered"&">Unanswered</a></li>"
Print #iFileNo, "                </ul>"
Print #iFileNo, "<div class="&"nav askquestion"&"></div>"
Print #iFileNo, "</body>"
Print #iFileNo, "</html>"

MsgBox "Done, html file has been created."

          'close the file (if you dont do this, you wont be able to open it again!)
      Close #iFileNo
End Sub
于 2013-01-13T18:13:24.293 回答