From: Alexis Laferrière Date: Sun, 21 May 2017 15:54:56 +0000 (-0700) Subject: dom: intro `XMLTag::attributes_to_map` mapping `XMLStringAttr` to their value X-Git-Url: http://nitlanguage.org dom: intro `XMLTag::attributes_to_map` mapping `XMLStringAttr` to their value Signed-off-by: Alexis Laferrière --- diff --git a/lib/dom/dom.nit b/lib/dom/dom.nit index 8c4c08e..5a1b8c0 100644 --- a/lib/dom/dom.nit +++ b/lib/dom/dom.nit @@ -67,3 +67,27 @@ redef class XMLStartTag abort end end + +redef class XMLAttrTag + + # Attributes as a map (ignoring malformed attributes) + # + # ~~~ + # var xml = """ + # + # """.to_xml + # + # var attributes = xml["student"].first.as(XMLAttrTag).attributes_to_map + # assert attributes.join(", ", ":") == "first:Snow, last:Man" + # ~~~ + fun attributes_to_map: Map[String, String] + do + var m = new Map[String, String] + for a in attributes do + if a isa XMLStringAttr then + m[a.name] = a.value + end + end + return m + end +end