2014-10-11 81 views
1

这个网站一直是我过去对许多vba问题的答案和想法的真正宝库,但是我一直无法找到任何有关我确信许多,如果不是大多数,在这个论坛这里是一个简单的任务。我必须处理很多xml报告文件,它们都有一个标题字符串,而我的问题是如何解析字符串以获取我需要的宏。 这是一个样本串:从一个没有分隔符的字符串中提取excel

<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

旧的时尚方式是使用instr来寻找开始。然后使用instr来查找结尾。然后用中间把它吸出来。

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) 

我没有测试它。

但我的空间分裂,然后通过阵列分裂=。这将给你一个数组名称为(0)和值(1)的2个元素数组。

但xml有它自己的查询语言和库来访问的东西。

这是一些代码分裂的命令行,然后分割成320×200 300和200

CmdLine = Command() 

A = Split(CmdLine, Chr(32), 2, 1) 
B = Split(A(0), "x", 2, 1) 
+0

感谢Oded,它的工作就像一个魅力!!!!!!!!!!!!这是最终结果:Sub Extract() Dim X(0 To 2)As String,Y(0 To 2)As String,FileType,DT,SN As Variant,B,E As Variant X(0)=“对于I = LBound(X(),1)到UBound(X(),1) B = InStr(1) ,范围(“A6”),值(X(i))+ Len(X(i))+ 1 E = InStr(B,范围(“A6”)值,“”) - 1 Y )=中等(范围(“A6”),值,B,E-B) Next [D1] =“Test =”&Y(0):[D2] =“测试:”&Left 1),10)&“at”&Mid(Y(1),12,8):[D2] = [D2]&“ - ”&Y(2) End Sub – Dan 2014-10-12 11:27:57

+0

并且在一些抛光后: – Dan 2014-10-12 12:29:48

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 
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 

我回答了你的qustions。另一人删除了两个更简单的方法。

问Oded把我对这段代码的解释。并恢复MS教程如何使用XML DOM对象进行操作。我展示了四种方式。

+0

您应该更多地解释这段代码如何工作。它让来自Stack Overflow的更多人更容易快速理解您的答案并解决他们的问题。 – JKor 2014-10-12 18:13:01

相关问题