One approach you can take is to loop through each node, and count the words if it's a text node, or loop through the child nodes if it's not.
This requires a few extra details, for example some elements you want to skip (<script> tags, for example), some you want the "value" instead, etc. But...