我正在尝试使用VBA解析xml以从电气测试系统获取测试结果。这是系统给我的一个例子:
<Reports>
<Report Type='UUT' Title='UUT Report' Link='-1-2013-11-20-10-2-46-867' UUTResult='Failed' StepCount='132'>
<Prop Name='UUT' Type='Obj' TypeName='UUT' Flags='0x0'>
<Prop Name='SerialNumber' Type='String' Flags='0x0'>
<Value>02</Value>
</Prop>
<Prop Name='PartNumber' Type='String' Flags='0x0'>
<Value>1009433</Value>
</Prop>
<Prop Name='LotNumber' Type='String' Flags='0x0'>
<Value>1234567</Value>
</Prop>
<Prop Name='CriticalFailureStack' Type='Array' LBound='[0]' HBound='[1]' ElementType='Obj' Flags='0x0'>
<ArrayElementPrototype Type='Obj' TypeName='NI_CriticalFailureStackEntry' Flags='0x0'>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value></Value>
</Prop>
<Prop Name='SequenceName' Type='String' Flags='0x0'>
<Value></Value>
</Prop>
<Prop Name='SequenceFileName' Type='String' Flags='0x0'>
<Value></Value>
</Prop>
<Prop Name='ResultId' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
</ArrayElementPrototype>
<Value ID='[0]'>
<Prop Type='Obj' TypeName='NI_CriticalFailureStackEntry' Flags='0x0'>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Brady Detection</Value>
</Prop>
<Prop Name='SequenceName' Type='String' Flags='0x0'>
<Value>MainSequence</Value>
</Prop>
<Prop Name='SequenceFileName' Type='String' Flags='0x0'>
<Value>1009450 AT ILR Final-test_app.seq</Value>
</Prop>
<Prop Name='ResultId' Type='Number' Flags='0x0'>
<Value>44</Value>
</Prop>
</Prop>
</Value>
<Value ID='[1]'>
<Prop Type='Obj' TypeName='NI_CriticalFailureStackEntry' Flags='0x0'>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Number of Brady Beats</Value>
</Prop>
<Prop Name='SequenceName' Type='String' Flags='0x0'>
<Value>Brady Detection</Value>
</Prop>
<Prop Name='SequenceFileName' Type='String' Flags='0x0'>
<Value>1009450 AT ILR Final-test_app.seq</Value>
</Prop>
<Prop Name='ResultId' Type='Number' Flags='0x0'>
<Value>49</Value>
</Prop>
我想从<prop name = 'serial number'>
中提取序列号,从<Report UUTResult = 'Failed'>
中提取测试结果,从<Prop Type='Obj' TypeName='NI_CriticalFailureStackEntry' Flags='0x0'> <Value>Brady Detection</Value>
提取设备故障
我之前从未使用过XML,但这是我到目前为止阅读本网站所得到的:
Dim xml As New MSXML.DOMDocument60
Dim xReport As IXMLDOMElement
Dim result As String
xml.Load (filePath)
Set xReport = xml.SelectSingleNode("//Reports/Report/")
result = xReport.getAttribute("UUTResult")
运行时,我收到“运行时错误'-2147467259(80004005)':设置xReport时出现自动化错误未指定错误。
我还尝试将xml转换为新的MSXML2.DOMDocument(而不是DOMDocument60),然后在同一行中出现类型不匹配。
非常感谢任何帮助!
感谢。
答案 0 :(得分:2)
以下是一种不使用MSXML
已经过测试
如果您的xml格式没有改变,那么它将为您提供所需的内容。
Option Explicit
Sub Sample()
Dim MyData As String, strData() As String
Dim i As Long
Dim sString As String
'~~> Replace your file here
Open "C:\Sample.xml" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
For i = LBound(strData) To UBound(strData)
If InStr(1, strData(i), "<Prop Name='SerialNumber'", vbTextCompare) Then
sString = strData(i + 1)
Exit For
End If
Next
If sString <> "" Then
Debug.Print Trim(Replace(Replace(sString, "<Value>", ""), "</Value>", ""))
End If
End Sub
<强>输出强>