如何查找XML子元素中是否存在具有特定值的属性?

时间:2013-08-27 12:30:20

标签: xml excel-vba vba excel

我试图找出xml文档根目录的每个子元素中是否存在具有特定值的XML属性。我的代码如下:

Option Explicit
Dim sheet As Worksheet
Dim rowCount1, i As Integer
Dim xNode As MSXML2.IXMLDOMNode
Dim xDoc As MSXML2.DOMDocument
Dim Nodes As MSXML2.IXMLDOMNodeList

Public Sub LoadDocument()
rowCount1 = 0
Set xDoc = New MSXML2.DOMDocument
xDoc.validateOnParse = False
 If xDoc.load("D:\Feedroutes.xml") Then ' The document loaded successfully.
  Set Nodes = xDoc.SelectNodes("//Property[@name='Value']")
  AttributesToColumns
  Else
 ' The document failed to load.
 End If
End Sub

Public Sub AttributesToColumns()
 Set sheet = ActiveSheet

 For Each xNode In Nodes
  If Not Nodes Is Nothing Then
   rowCount1 = rowCount1 + 1
   sheet.Cells(rowCount1, 3).Value = xNode.Text
  'I want to increment the rowCount even if the attribute with the
  'specific value doesn't exist
  ElseIf Nodes Is Nothing Then
  rowCount1 = rowCount1 + 1

  End If
 Next xNode
End Sub

目前它只在连续的行中写入属性的值,而所需的输出是,只在具有特定值的属性存在的行中写入。我的XML看起来像这样:

<Tag name="PosLim" path="Feeders/R1" type="OPC">
  <Property name="Value">0.0</Property>
  <Property name="DataType">4</Property>
  <Property name="OPCServer">Ignition OPC-UA Server</Property>
  <Property name="OPCItemPath">[Siemens]DB141,I74</Property>
  <Property name="ScaleMode">1</Property>
  <Property name="RawHigh">1000.0</Property>
  <Property name="FormatString">#,##0.00</Property>
  <Property name="EngUnit">Kg</Property>
</Tag>
<Tag name="JogSettle" path="Feeders/R1" type="OPC">
  <Property name="DataType">1</Property>
  <Property name="OPCServer">Ignition OPC-UA Server</Property>
  <Property name="OPCItemPath">[Siemens]DB141,I96</Property>
  <Property name="RawHigh">1000.0</Property>
  <Property name="FormatString">#,##0</Property>
  <Property name="EngUnit">S</Property>
</Tag>
<Tag name="Positive Tol" path="Feeders/R1" type="OPC">
  <Property name="Value">0.0</Property>
  <Property name="DataType">4</Property>
  <Property name="OPCServer">Ignition OPC-UA Server</Property>
  <Property name="OPCItemPath">[Siemens]DB141,I78</Property>
  <Property name="ScaleMode">1</Property>
  <Property name="RawHigh">1000.0</Property>
  <Property name="FormatString">#,##0.00</Property>
  <Property name="EngUnit">Kg</Property>
</Tag>

我的目的是编写属性“name”的值,其中name =“Value”以及所有其他属性以及相应的行,而不是连续的行。所以从上面的xml文件中,我的excel输出应该是:

   Row No.   Value        ScaleMode 
   Row1        0             1 
   Row2 
   Row3        0             1 etc

1 个答案:

答案 0 :(得分:3)

您的Nodes值只有名称为Value的Property节点,从中您无法确定要跳过哪些节点。您需要做的是选择所有Tag节点,然后检查该Tag是否具有name = Value的属性。

首先添加一行来保存您的Property标记

Dim propertyNode As MSXML2.IXMLDOMNode

然后,在LoadDocument Sub中,选择所有标记节点(而不是选择名称=值的节点)

Set Nodes = xDoc.SelectNodes("//Tag")

现在您已拥有所有标记,您可以跳过任何没有name = Value属性的标记。使用以下内容替换For Sub中AttributesToColumns语句中的代码。

Set propertyNode = xNode.SelectSingleNode("Property[@name='Value']")
If Not propertyNode Is Nothing Then sheet.Cells(rowCount1, 3).Value = propertyNode.Text
rowCount1 = rowCount1 + 1