Cycle through all (unknown node structure) xml nodes

How should I format a for loop, or while conditional, to cycle through all of the nodes, node levels in a unknown XML?

For instance, if I load an XML:


<root>
	<word>
		<word>
			<word>
				<image src="some address" />
			</word>
		</word>
			<word></word>
		<word>
		</word>
		<word>
		</word>
	</word>
</root>

There are 3 levels, not couting root. I could write a triple for loop, but that would only work in a 3 level xml. How to write a loop that would cycle through even XX-levels of nesting?