admin管理员组

文章数量:1399339

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?

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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 3, 2013 at 18:14 AlexRAlexR 5,6449 gold badges79 silver badges131 bronze badges 1
  • 1 Check whether the xml file has been saved with a BOM. – RobH Commented Nov 3, 2013 at 19:55
Add a ment  | 

3 Answers 3

Reset to default 7

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.

I ran into this issue too, I fixed it by removing the BOM (Byte order mark) from the document in my editor. This fixed the problem for me. I guess the parser sees the BOM as content or something.

I had the same problem, I've downloaded Notepad++ and configured the encoding to 'Encode in UTF-8'. You can find it by opening your xml file and selecting Encoding tab -> 'Encode in UTF-8'.

It'll be effective for all xml files in the future.

Hope it finds you well.

本文标签: javascriptGoogle Apps Script How to fix quotcontent is not allowed in prologquotStack Overflow