2013-08-27 42 views
2

我想知道在xml文檔根目錄的每個子元素中是否存在具有特定值的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 =「值」和所有其他屬性以及在相應的行,不連續的行。所以從上面的XML文件,我的Excel的輸出應該是:

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

回答

3

Nodes值只有有一個名稱=值的屬性節點,並從,你不能確定哪些節點被跳過。你需要做的是選擇所有的標籤節點,然後檢查該標籤是否具有name = Value屬性。

首先添加額外的線牽你的屬性標記

Dim propertyNode As MSXML2.IXMLDOMNode

然後,在你LoadDocument子,選擇所有標籤節點(而不是與名稱=值選擇節點)

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

現在您已擁有所有標籤,可以跳過沒有名稱=值屬性的任何標籤。將AttributesToColumns Sub中的For聲明中的代碼替換爲以下代碼。

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

嗨Jaycal,它的作品!非常感謝您提供清晰準確的答案。非常感激。沒有你的時間和精力就沒有可能。必須學習更多關於XPath和XML的知識。 – user2093481

相關問題