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?
- 1 Check whether the xml file has been saved with a BOM. – RobH Commented Nov 3, 2013 at 19:55
3 Answers
Reset to default 7It 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744211502a4563353.html
评论列表(0条)