Я пытаюсь написать сценарий Excel VBA для выборочного получения информации из файла xml и создания таблицы с этой информацией.
Есть тысячи узлов, все дефекты имен, и в каждом дефекте есть атрибуты. Я хочу использовать атрибут в каждом дефекте, чтобы отфильтровать некоторые нежелательные дефекты, и перечислить все остальные атрибуты желаемых дефектов в узлах с именем дефект.
Пожалуйста, смотрите ниже для получения дополнительной информации. nextSibling нельзя использовать.
XML-структура:
<!-- language: xml -->
<?xml version="1.0" encoding="WINDOWS-1252"?>
<CDXML>
<DATA><FaceInformation><Defects>
<Defect>
<Black Type="Integer" Value="1"/>
<Dots Type="Integer" Value="21"/>
<Height Type="Integer" Value="1"/>
</Defect>
<Defect>
<Black Type="Integer" Value="0"/>
<Dots Type="Integer" Value="22"/>
<Height Type="Integer" Value="2"/>
</Defect>
<Defect>
<Black Type="Integer" Value="1"/>
<Dots Type="Integer" Value="23"/>
<Height Type="Integer" Value="3"/>
</Defect>
</Defects></FaceInformation></DATA>
Желаемый результат:
<!-- language: lang-none -->
Black Dots Height
1 21 1
1 22 3
VBA:
<!-- language: lang-vb -->
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
Dim n, m As IXMLDOMNode
Dim j As Integer
XMLFileName = C:\1.xml
xmlDoc.Load (XMLFileName)
'This goes through all the attributes with the name Black,
'this is okay since Black only appear once in each node called defect.
For Each n In xmlDoc.SelectNodes("//Defects/Defect/Black")
'Read value of the Black attribute
Black = n.Attributes.getNamedItem("Value").Text
If Black <> "0" Then 'Print if 1
Cells(j + 3, 2) = Black
'I can not use nextSibling because the order of defects is not always the same
For Each m In n.SelectNodes("DefectClass")
Dots = m.Attributes.getNamedItem("Value").Text
'I have no idea what to do from this point
Cells(i + 3, 3) = Dots
Next
j = j + 1
End If
Next