. . . "XML::Twig (mentioned above) is specifically designed for the sort of task you are asking about - setting up a bunch of rules to handle incoming XML features and then streaming some XML through them." .