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 を使用して、そのファイル内の 2 つのレイヤー間。

結果は次のようになります。

  <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 に答える