lib/dom: intro `XMLEntity::[]`
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 26 Jun 2015 17:29:22 +0000 (13:29 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 30 Jun 2015 16:37:59 +0000 (12:37 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

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