views:

413

answers:

1

I'm trying to fetch and parse an XML-file through javascript. I don't control the XML-file.

Now somehow the encoding of some XML-files changed, which results in the code not being able to parse the file as far as I can tell. It used to be ANSI, some are Unicode now (and those are failing). Is there a way for me to correctly get the content, so both versions (ANSI and Unicode) work?

Files just start with: <?xml version="1.0"?>

And the only thing in javascript to to parse is:

var parser = new DOMParser();
var dom = parser.parseFromString(responseDetails.responseText,"application/xml");

jQuery-answers are fine btw, not using it for this part but it is available.

+1  A: 

If the encoding isn't correctly specified, I think you're going to have to chop the header off, then attach a new header specifying a candidate encoding. Parse that, and if it fails, attach a new header with a new candidate encoding. And so on.

Of course, a successful parse doesn't imply you've got the right encoding, but an encoding that passes the parsing stage.

The real fix is to correct the original XML, unfortunately.

Brian Agnew