in reply to XML::TreeBuilder invalid token problem

I would interpret &%2339; as a failed attempt to encode the apostrophe character, as follows:
  1. apostrophe ('  = 0x27) is 39 decimal, so ' is the (decimal) numeric character entity for that
  2. pound (#  = 0x23) gets converted to %23, yielding &%2339;

Since the result of step 2 is an invalid entity reference, either step 2 should not have been done (leaving ' as-is), or the remaining ampersand should have been converted as well, to yield %26%2339; (update: or perhaps it should have been rendered as  &%2339;)

The whole mess could have been avoided if the original apostrophe had been converted to %27, though I'm not sure from your description whether this would actually work either...

Another update: As for actually dealing with that, maybe you want to "pre-condition" the text before passing it to XML::TreeBuilder -- e.g. if you have the whole xml string in a scalar called "$text", you could do this:

$text =~ s/\%([0-9A-F]{2})/chr(hex($1))/eg;
(or be more particular/ad-hoc, and just do  s/\%23/\#/g;) Then pass $text to Treebuilder. That might put things right.