I think you need to take a step back and answer the question, "Why do bare ampersands (&) and HTML/SGML/XML entities co-exist in this file?"
If the answer is that you are trying to encode characters in the file, then I have two answers for you:
- Check CPAN for code to do this for you.
- If you feel you need to code this yourself, the first step should be to convert ampersands to the ampersand entity (probably &). At that point, there will no longer be any "bare ampersands" in the file and you may safely proceed with converting other characters to their respective entities.
If you have some other answer to my question, you need to explain that in your question.