查找xml节点和子属性

问题描述 投票:0回答:1

我需要找到Dataset的节点名称及其子节点的所有属性。所以我用下一个循环来查找FCDA的值及其属性。但是代码不起作用。你能帮帮我吗,我错了。

CML日期

<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
xml vba
1个回答
1
投票

AnalystCave有一个很好的教程:Working with XML files in VBA (VBA XML)

方法1

从Root节点向下钻取到最后一个叶子。每个嵌套的For Each循环都应该遍历它上面的For Each循环的ChildNodes。

  • XDoc.DocumentElement:根节点
  • XDoc.DocumentElement:根节点ChildNodes:数据集
  • XDoc.DocumentElement:根节点ChildNodes:数据集ChildNodes:FCDA节点

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

方法2

迭代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
© www.soinside.com 2019 - 2024. All rights reserved.