AlexR AlexR - 7 months ago 132
Javascript Question

Google Apps Script: How to fix "content is not allowed in prolog"

I am trying to parse the following XML file with Google Apps Script

XmlService
:

<?xml version="1.0" encoding="UTF-8"?>
<Report Major="1" Minor="0" Revision="1">
<CoIDs> ….


Code snippet:

function parse(txt) {
var document = XmlService.parse(txt);
var root = document.getRootElement();
//...
}


When running the script, I get the error message:
Content is not allowed in prolog
.

Is there a issue with the format of the XML file?
How can I parse this file with Google Apps Script?

Update



I managed to solve the issue by opening the file and saving it again as
UTF-8
document with Apple TextEdit. Is there any "automatic" (or code-based) way to convert a non-
UTF 8
(presumably
UTF-16
document) to
UTF-8
before reading it with Google Apps Script?

Answer

It is possible to choose the char set of a file when you open it in your drive, I found this info in a post answer by a Google engineer (Corey G) so even if I didn't test it I think he's a trustful source :-) .

The post is here and the code goes as follow:

DocsList.getFileById(<some id>).getBlob().getDataAsString("UTF-16");// replace by the Charset you want... UFT-8 for example.

So I guess it's worth trying... Let us know if it solves your problem.