Johan
Johan

Reputation: 3212

How to parse xml in JavaScript?

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");

Upvotes: 0

Views: 1752

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272357

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.

Upvotes: 1

Related Questions