我需要找到Dataset的节点名称及其子节点的所有属性。所以我用下一个循环来查找FCDA的值及其属性。但是代码不起作用。我可以帮助我,我错了。
<DataSet name="StatUrgA" desc="Primary Switch and General Status">
<FCDA lnClass="LLN0" prefix="" doName="Loc" ldInst="LD1" fc="ST" />
<FCDA lnClass="GGIO" prefix="ENMC" doName="Ind2" lnInst="1" ldInst="LD1" fc="ST" />
<FCDA lnClass="GGIO" prefix="ENMC" doName="Ind3" lnInst="1" ldInst="LD1" fc="ST" />
<FCDA lnClass="GGIO" prefix="ENMC" doName="Ind1" lnInst="1" ldInst="LD1" fc="ST" />
<FCDA lnClass="XCBR" prefix="CB" doName="BlkCls" lnInst="10" ldInst="LD1" fc="ST" />
<FCDA lnClass="XCBR" prefix="CB" doName="BlkOpn" lnInst="10" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="CB" doName="Pos" lnInst="10" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="DCO" doName="BlkCls" lnInst="15" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="DCO" doName="BlkOpn" lnInst="15" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="DCO" doName="Pos" lnInst="15" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="ESW" doName="BlkCls" lnInst="16" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="ESW" doName="BlkOpn" lnInst="16" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="ESW" doName="Pos" lnInst="16" ldInst="LD1" fc="ST" />
<FCDA lnClass="GGIO" prefix="PS" doName="DPCSO" lnInst="2" ldInst="LD0" fc="ST" />
<FCDA lnClass="GGIO" prefix="PS" doName="DPCSO" lnInst="1" ldInst="LD0" fc="ST" />
<FCDA lnClass="LPHD" prefix="" doName="InOv" lnInst="1" ldInst="LD1" fc="ST" />
<FCDA lnClass="LPHD" prefix="" doName="InOv" lnInst="1" ldInst="LD0" fc="ST" />
</DataSet>
<DataSet name="StatIed" desc="Status">
<FCDA lnClass="LLN0" prefix="" doName="Beh" ldInst="LD1" fc="ST" />
<FCDA lnClass="XCBR" prefix="CB" doName="Beh" lnInst="10" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="CB" doName="Beh" lnInst="10" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="DCO" doName="Beh" lnInst="15" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="DCO" doName="Beh" lnInst="15" ldInst="LD1" fc="ST" />
<FCDA lnClass="XSWI" prefix="ESW" doName="Beh" lnInst="16" ldInst="LD1" fc="ST" />
<FCDA lnClass="CSWI" prefix="ESW" doName="Beh" lnInst="16" ldInst="LD1" fc="ST" />
<FCDA lnClass="LPHD" prefix="" doName="PhyHealth" lnInst="1" ldInst="LD1" fc="ST" />
</DataSet>
<DataSet name="MeasFlt" desc="Measurands">
<FCDA lnClass="MMXU" prefix="UI" doName="TotW" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="PhV" lnInst="2" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="A" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="PhV" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="PPV" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="Hz" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="TotPF" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="TotVA" lnInst="1" ldInst="LD1" fc="MX" />
<FCDA lnClass="MMXU" prefix="UI" doName="TotVAr" lnInst="1" ldInst="LD1" fc="MX" />
</DataSet>
Dim strXml As String
Dim XDoc As Object, root As Object
Filename = ThisWorkbook.Sheets("CID").Cells(1, 1).Value
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
strXml = XDoc.Load(Filename)
Set root = XDoc.DocumentElement
For Each datasetnodes In XDoc.ChildNodes
For Each fcdanodes In XDoc.ChildNodes
Set List1 = XDoc.SelectNodes("//FCDA")
ThisWorkbook.Sheets("Datasets").Cells(i + 2, 3).Value = List1(i).getAttribute("ldInst")
MsgBox List1(i).getAttribute("ldInst")
i = i + 1
Next
Next
答案 0 :(得分:1)
AnalystCave有一个很好的教程:Working with XML files in VBA (VBA XML)。
从Root节点向下钻取到最后一个叶子。每个嵌套的For Each
循环都应遍历其上方For Each
循环的ChildNodes。
Set root = XDoc.DocumentElement
For Each datasetnodes In root.ChildNodes
For Each fcdanodes In datasetnodes.ChildNodes
ThisWorkbook.Sheets("Datasets").Cells(i + 2, 3).Value = fcdanodes.getAttribute("ldInst")
i = i + 1
Next
Next
迭代FCDA
XDoc.SelectNodes("//FCDA")
个节点集合
Set root = XDoc.DocumentElement
For Each fcdanodes In XDoc.SelectNodes("//FCDA")
ThisWorkbook.Sheets("Datasets").Cells(i + 2, 3).Value = fcdanodes.getAttribute("ldInst")
i = i + 1
Next