lib/dom: intro `XMLEntity::[]`
[nit.git] / lib / dom / dom.nit
index 9234057..273941a 100644 (file)
 module dom
 
 import parser
+
+redef class XMLEntity
+
+       # The `XMLTag` children with the `tag_name`
+       #
+       # ~~~
+       # var code = """
+       # <?xml version="1.0" encoding="us-ascii"?>
+       # <animal>
+       #     <cat/>
+       #     <tiger>This is a white tiger!</tiger>
+       #     <cat/>
+       # </animal>"""
+       #
+       # var xml = code.to_xml
+       # assert xml["animal"].length == 1
+       # assert xml["animal"].first["cat"].length == 2
+       # ~~~
+       fun [](tag_name: String): Array[XMLEntity]
+       do
+               var res = new Array[XMLEntity]
+               for child in children do
+                       if child isa XMLTag and child.tag_name == tag_name then
+                               res.add child
+                       end
+               end
+               return res
+       end
+end