AlexR AlexR - 8 months ago 154
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


<?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?


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


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.