2014-03-04 38 views
0

我可能在某處忽略了它,但通過Dart版本的PetitParser獲取特定名稱的所有元素(類似於舊的getElementsByTagName)的好方法是什麼?如何使用Dart Petitparser XML解析器執行「getElementsByTagName」?

我設法加載一個XML文件,並使用PetitParser成功解析它,但現在我想通過具有特定名稱的所有節點(例如,參見下面的節點與「importantData」)。

的result.value.length也似乎是非常高的(16654)對665 「importantData」 從我測試xml文件;這些都是result.value.children節點[1]。兒童

<xml> 
    <toplevel> 
    <importantData> 
     <attribute1>Value</attribute1> 
     <attribute2>Value</attribute2> 
    </importantData> 
    <importantData> 
     <attribute1>Value</attribute1> 
     <attribute2>Value</attribute2> 
    </importantData> 
    <importantData> 
     <attribute1>Value</attribute1> 
     <attribute2>Value</attribute2> 
    </importantData> 
    ... 
    </toplevel> 
</xml> 

回答

1

XmlNode是一個Iterable<XmlNode>所有的子女。

如果root是你的XML樹的解析根節點你可以寫:

for (var node in root) { 
    if (node is XmlElement && node.name.local == 'importantData') { 
    // do something with the node 
    } 
} 

如果你更成函數式編程,您可以使用下面的表達式返回一個迭代在對有關的所有元素:

root.where((node) => node is XmlElement && node.name.local == 'importantData') 
+0

感謝堆盧卡斯,這正是我以後的! – Geert

相關問題