If we are to extrapolate this toy-example into a requirement of any size (and, mind you, you might not), then I will hands-down suggest that you use a library that supports XSLT XPath ... and that you use this technique to retrieve results from your XML structure, instead of cobblestoned logic of your own. CPAN has a plentitude of industrial-strength XML tools, they’re quite efficient, and I feel that they consistently deliver more maintainable and flexible solutions. Build an XPath expression that describes what you want, and let its fingers do the walking for you.