Handle encoded characters by setting parsed encoding and output encoding

At the moment encoded characters get broken when using dom document. This PR fixes it so that these characters are encoded correctly.