При этом используется синтаксический анализатор DOM для записи значений и продолжительности после чтения каждого раздела HTML из HTML-файла.Я боролся с доступом к узлам текста, поэтому мне пришлось взломать то, что меня не устраивает.Это означает, что вы знаете искомое значение, что означает, что вы можете выполнить замену строки html с помощью метода regex .Replace.Для dStartTime и dendTime вы можете выполнить прямую замену внешнего HTML.Это заменит только в текущем HTMLDocument, если вы не записываете обратно в файл.SetAttribute не работает в этом случае.Я не уверен, почему я не смог получить доступ к текстовым узлам, особенно из-за отсутствия исходного HTMLDocument.Было бы хорошо, если бы кто-то мог решить это.
item.outerHTML = Replace$(item.outerHTML, "dstarttime=" & Chr$(34) & item.dstarttime & Chr$(34), "dstarttime=" & 666 & Chr$(34)) 'example 666 as replace value.
vba:
Option Explicit
Public Sub GetValues()
'VBE > Tools > References > HTML Object Library
Dim html As HTMLDocument
Debug.Print "doc1", vbNewLine
Set html = GetHTMLFileContent("C:\Users\User\Desktop\test.html") 'first html document. Info saved in text file with .html extension
GetItems html
Debug.Print "doc2", vbNewLine
Set html = GetHTMLFileContent("C:\Users\User\Desktop\test2.html") 'second html document. Info saved in text file with .html extension
GetItems html
End Sub
Public Sub GetItems(ByVal html As HTMLDocument)
Dim items As Object, item As Object, counter As Long
Set items = html.getElementsByTagName("item")
For Each item In items
Debug.Print " item " & counter + 1
Dim itemArr() As String
itemArr = Split(html.body.innerHTML, "</ITEM")
Debug.Print "startTime = " & Round(item.dStartTime, 2), "endTime = " & Round(item.dendTime, 2), "duration : " & Round(item.dendTime - item.dStartTime, 2)
Debug.Print "Associated text numbers "
On Error Resume Next
GetTextAttributeNumbers Split(itemArr(counter), "<TEXT>")(1)
On Error GoTo 0
counter = counter + 1
Next item
End Sub
Public Sub GetTextAttributeNumbers(ByVal inputString As String)
Dim matches As Object, iMatch As Object
With CreateObject("vbscript.regexp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "\d{1,}(\.\d+)?"
If .TEST(inputString) Then
Set matches = .Execute(inputString)
For Each iMatch In matches
Debug.Print iMatch
Next iMatch
End If
End With
End Sub
Public Function GetHTMLFileContent(ByVal filePath As String) As HTMLDocument
'"C:\Users\HarrisQ\Desktop\test.html"
Dim fso As Object, hFile As Object, hString As String, html As New HTMLDocument
Set fso = CreateObject("Scripting.FileSystemObject")
Set hFile = fso.OpenTextFile(filePath)
Do Until hFile.AtEndOfStream
hString = hFile.ReadAll()
Loop
html.body.innerHTML = hString
Set GetHTMLFileContent = html
End Function
Для правильного разбора документов требуется следующий синтаксис:
Doc1
<html>
<head></head>
<body>
<item dstarttime="4" dendtime="8" n3drhythm="2" str3dscenelayoutfile="">
<text>
Batti Gul Meter Chalu
</text>
</item>
<item dstarttime="9.52" dendtime="14.47" n3drhythm="2" str3dscenelayoutfile="">
<text>
rajj<10.44> ke<10.99> rulaya
</text>
</item>
<item dstarttime="14.47" dendtime="19.06" n3drhythm="2" str3dscenelayoutfile="">
<text>
rajj ke<15.94> hansaya
</text>
</item>
</body>
</html>
Doc2
<html>
<head></head>
<body>
<item dstarttime="2" dendtime="4" n3drhythm="2" str3dscenelayoutfile="">
<text>
Batti Gul Meter Chalu
</text>
</item>
<item dstarttime="4.76" dendtime="7.24" n3drhythm="2" str3dscenelayoutfile="">
<text>
rajj<5.22> ke<5.50> rulaya
</text>
</item>
<item dstarttime="7.24" dendtime="9.53" n3drhythm="2" str3dscenelayoutfile="">
<text>
rajj ke<7.97> hansaya
</text>
</item>
</body>
</html>
Это означает, что, если его нет, добавить в ряд отсутствующие начальную и конечную строки.
"<html><head></head><body>" & yourHTMLString & "</body>
</html>"
Пример выводаиз второго документа:
![enter image description here](https://i.stack.imgur.com/2udo9.png)