这个网站过去一直是我的许多vba问题的答案和想法的真正宝库,但我无法找到任何关于我确信对于许多人,如果不是大多数,在这里论坛一个简单的任务。我必须处理大量的xml报告文件,这些文件都有一个标题字符串,我的问题是如何解析我需要的nuggest字符串。 这是一个示例字符串:
<Function IDREF="TST_RxRccsMatrix_Rx64" Start="2011-04-07T14:21:35.593000+02:00" Status="Success" Tags="SystemSerialNumber:41009" End="2011-04-07T14:29:16.625000+02:00">
我需要提取 - 报告类型:TST_RxRccsMatrix(此字符串的长度不是常量) - 开始日期 - 时间戳:2011-04-07T14:21:35.593000 + 02:00(长度不变) - 序列号:41009(长度不变)
我尝试过使用Split和InStr和Find的方法,但是没有一个方法可以为所有三个提取产生所需的结果。
我非常感谢你们的任何帮助!
答案 0 :(得分:0)
旧的时尚方式是用instr来寻找开始。然后使用instr查找结尾。然后用mid来吸掉它。
Begin = instr(1,xmlstring,"IDREF=") + Len("IDREF")
'look for first space after IDREF= in string
End = instr(Begin, xmlstring, " ")
Report = mid(xmlstring, begin, end - begin)
我没有测试它。
但我在空间上分裂,然后通过=上的阵列分裂。这将为您提供一个包含2个元素数组的数组,其值为(0),值为(1)。
但xml拥有自己的查询语言和库来访问内容。
这是一些代码拆分命令行,然后将320x200拆分为300和200。
CmdLine = Command()
A = Split(CmdLine, Chr(32), 2, 1)
B = Split(A(0), "x", 2, 1)
答案 1 :(得分:0)
经过一些抛光:
Private Sub GetFileInfo()
Dim fso As New FileSystemObject, strText As Variant, i As Integer
Dim X(0 To 2) As String, Y(0 To 2) As String, B, E As Variant
'get header string from xml file
'FName (file name) was ascertained by a previous sub and is made public
Set strText = fso.OpenTextFile(FName, ForReading, False)
'header string is in second (i = 2) line of file
For i = 1 To 2: [A1] = strText.ReadLine: Next: strText.Close: Set fso = Nothing
'User Oded's search and extract routine
X(0) = "IDREF=": X(1) = "Start=": X(2) = "Tags="
For i = LBound(X(), 1) To UBound(X(), 1)
B = InStr(1, [A1], X(i)) + Len(X(i)) + 1 ' + 1 includes trailing " character
E = InStr(B, [A1], " ") - 1 ' - 1 includes leading " character
'required if a search string in X() is at the end of the header which ends with a ">"
If (InStr(B, [A1], " ") - 1) < 0 Then E = InStr(B, [A1], ">")
Y(i) = Mid([A1], B, E - B)
Next
[D1] = "Test = " & Y(0)
[D2] = "Tested on : " & Left(Y(1), 10) & " at " & Mid(Y(1), 12, 8)
[D2] = [D2] & " - " & Y(2)
End Sub
答案 2 :(得分:0)
xmlstring = "<Function IDREF=""TST_RxRccsMatrix_Rx64"" Start=""2011-04-07T14:21:35.593000+02:00"" Status=""Success"" Tags=""SystemSerialNumber:41009"" End=""2011-04-07T14:29:16.625000+02:00"">"
Set regEx = New RegExp
regEx.Pattern = "IDREF=""([a-z0-9_]+)"""
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(xmlstring)
If Matches.count <> 1 then msgbox "no match or too many"
For Each Match in Matches
Msgbox match.submatches(0)
Next
我回答了你的问题。另一个人删除了两种更简单的方法。
要求Oded放回我对此代码的解释。并恢复关于如何使用XML DOM对象的MS教程。我展示了四种方式。