http://jsfiddle.net/JamesKyle/L4b8b/
This may be a futile effort, but I personally think its possible.
I\'m not the best at Java
If you're doing this client-side, and you already have the DOM, then it would be more efficient to serialise it yourself inserting the appropriate tags as you go rather than serialising the whole subtree at once and then trying to reparse it.