I am trying to parse the following XML file with Google Apps Script XmlService
:
我正在尝试用谷歌应用程序脚本XmlService解析以下XML文件:
<?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
.
在运行脚本时,我得到了错误消息:prolog中不允许有内容。
Is there a issue with the format of the XML file? How can I parse this file with Google Apps Script?
XML文件的格式有问题吗?如何使用谷歌应用程序脚本解析这个文件?
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?
通过打开文件并将其保存为使用Apple TextEdit的UTF-8文档,我成功地解决了这个问题。在使用谷歌应用程序脚本读取非utf 8(假定是UTF-16文档)之前,是否有“自动”(或基于代码)方法将其转换为UTF-8 ?
3 个解决方案
#1
4
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 :-) .
当你在驱动器中打开文件时,可以选择文件的char集合,我在谷歌工程师(Corey G)的一个帖子中找到了这个信息,所以即使我没有测试它,我也认为他是一个可靠的来源:-)。
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.
所以我想这值得一试。如果它能解决你的问题,请告诉我们。
#2
1
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.
我也遇到了这个问题,我通过从编辑器中的文档中删除BOM(字节顺序标记)来修复它。这就解决了我的问题。我猜解析器将BOM视为内容或其他东西。
#3
0
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'.
我也遇到了同样的问题,我下载了Notepad++,并将编码配置为“UTF-8编码”。您可以通过打开xml文件并选择编码选项卡—> 'Encode in UTF-8'找到它。
It'll be effective for all xml files in the future.
它在将来对所有xml文件都有效。
Hope it finds you well.
希望它能找到你。
#1
4
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 :-) .
当你在驱动器中打开文件时,可以选择文件的char集合,我在谷歌工程师(Corey G)的一个帖子中找到了这个信息,所以即使我没有测试它,我也认为他是一个可靠的来源:-)。
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.
所以我想这值得一试。如果它能解决你的问题,请告诉我们。
#2
1
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.
我也遇到了这个问题,我通过从编辑器中的文档中删除BOM(字节顺序标记)来修复它。这就解决了我的问题。我猜解析器将BOM视为内容或其他东西。
#3
0
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'.
我也遇到了同样的问题,我下载了Notepad++,并将编码配置为“UTF-8编码”。您可以通过打开xml文件并选择编码选项卡—> 'Encode in UTF-8'找到它。
It'll be effective for all xml files in the future.
它在将来对所有xml文件都有效。
Hope it finds you well.
希望它能找到你。