This is probably the easiest way to write a script with using Nmap::Parser, if you don't need the general scan information. During the parsing process, the parser will obtain information of every host from the xml scan output. The callback function is called after completely parsing a single host. When the callback returns (or you finish doing what you need to do for that host), the parser will delete all information of the host it had sent to the callback. This callback function is called for every host that the parser encounters.
(1)
]
