如何解析存储在我的 Google 驱动器中但以 html 类型突出的 XML 文件?!
我在我的 google Drive 云上保存了源的 xml 副本:http://api.allocine.fr/rest/v3/movie?media=mp4-lc&partner=YW5kcm9pZC12Mg&profile=large&version=2&code=265621 我可以解析源代码,但无法解析看起来像 html 类型的 xml 副本! 我有解析错误,例如:元素类型“meta”必须由匹配的结束标记“”终止 或元素类型“a.length”后必须跟有属性规范“">”或“/>” 我在https://drive.google.com/file/d/16kJ5Nko-waVb8s2T12LaTEKaFY01603n/view?usp=sharing上分享了它为您提供访问权限并测试我的脚本。 我知道我可以使用cacheService并且它可以工作,但是为了对缓冲进行其他控制,我会尝试这种方式
function xmlParsingXmlStoreOnGoogleDrive(){
//So , this is the original xml that is good parsed
var fetched=UrlFetchApp.fetch("http://api.allocine.fr/rest/v3/movie?media=mp4-lc&partner=YW5kcm9pZC12Mg&profile=large&version=2&code=265621")
var blob=fetched.getBlob();
var getAs=blob.getAs("text/xml")
var data=getAs.getDataAsString("UTF-8")
Logger.log(data.substring(1,350)); // substring to not saturate the debug display this expected code XML:
/*
?xml version="1.0" encoding="utf-8"?>
<!-- Copyright © 2019 AlloCiné -->
<movie code="265621" xmlns="http://www.allocine.net/v6/ns/">
<movieType code="4002">Long-métrage</movieType>
<originalTitle>Mise à jour sur Google play</originalTitle>
<title>Mise à jour sur Google play</title>
<keywords>Portrait of a Lady on Fire </keywords>
*/
var xmlDocument=XmlService.parse(data);
var root=xmlDocument.getRootElement();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log(keywords); // Display the expected result :"Portrait of a Lady on Fire "
// And this my copie of the original xml, that i can't parsing
var fetched=UrlFetchApp.fetch("https://drive.google.com/file/d/1K3-9dHy-h0UoOOY5jYfiSoYPezSi55h1/view?usp=sharing")
var blob=fetched.getBlob();
var getAs=blob.getAs("text/xml")
var data=getAs.getDataAsString("UTF-8")
Logger.log(data.substring(1,350)); // substring to not saturate the debug display this non expected code HTML !:
/*
!DOCTYPE html><html><head><meta name="google" content="notranslate"><meta http-equiv="X-UA-Compatible" content="IE=edge;">
<style>@font-face{font-family:'Roboto';font-style:italic;font-weight:400;src:local('Roboto Italic'),local('Roboto-Italic'),
url(//fonts.gstatic.com/s/roboto/v18/KFOkCnqEu92Fr1Mu51xIIzc.ttf)format('truetype');}@font-face{font-fam......
*/
var xmlDocument=XmlService.parse(data); // ABORT WITH THE ERROR: Element type "a.length" must be followed by either attribute specifications, ">" or "/>"
var root=xmlDocument.getRootElement();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log(keywords);
}
我读到了类似的问题:Parse XML file (which is stored on GoogleDrive) with Google app script
“不幸的是,我们无法直接获取 google 驱动器中的 xml 文件”! 这是正确的吗?这是否仅仅意味着我无法实现我的脚本?
最佳答案
- 您想要从 Google 云端硬盘上的文件中检索数据并使用 XmlService 解析为 XML 数据。
- 您希望使用 Google Apps 脚本来实现此目的。
如果我的理解是正确的,这个答案怎么样?
修改点:
- 关于
var fetched=UrlFetchApp.fetch("https://drive.google.com/file/d/16kJ5Nko-waVb8s2T12LaTEKaFY01603n/view?usp=sharing")
,在这种情况下,无法从此端点检索文件内容。如果您想使用UrlFetchApp检索文件内容,请使用https://drive.google.com/uc?id=16kJ5Nko-waVb8s2T12LaTEKaFY01603n&export=download
端点。这是 webContentLink。 - 当文件位于您的 Google 云端硬盘中和/或公开共享时,您可以使用脚本
DriveApp.getFileById(fileId).getBlob().getDataAsString()
检索数据。 .
修改后的脚本:
例如,当您共享样本文件 https://drive.google.com/file/d/16kJ5Nko-waVb8s2T12LaTEKaFY01603n/view?usp=sharing
使用后,脚本变为如下。
示例脚本 1:
在此模式中,文件内容是通过 UrlFetchApp.fetch()
从共享文件中检索的。 .
var data = UrlFetchApp.fetch("https://drive.google.com/uc?id=16kJ5Nko-waVb8s2T12LaTEKaFY01603n&export=download").getContentText(); // Modified
var xmlDocument=XmlService.parse(data);
var root=xmlDocument.getRootElement();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log(keywords); // <--- You can see "Portrait of a Lady on Fire" at log.
- 在这种情况下,需要公开共享脚本。如果您想检索文件内容而不共享,请使用访问 token 进行请求。
示例脚本 2:
在此模式中,文件内容是通过 DriveApp.getFileById()
从共享文件中检索的。 .
var fileId = "16kJ5Nko-waVb8s2T12LaTEKaFY01603n"; // Added
var data = DriveApp.getFileById(fileId).getBlob().getDataAsString(); // Added
var xmlDocument=XmlService.parse(data);
var root=xmlDocument.getRootElement();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log(keywords); // <--- You can see "Portrait of a Lady on Fire" at log.
-
16kJ5Nko-waVb8s2T12LaTEKaFY01603n
的https://drive.google.com/file/d/16kJ5Nko-waVb8s2T12LaTEKaFY01603n/view?usp=sharing
是文件 ID。 - 在这种情况下,不需要共享文件。但该文件必须位于您的 Google 云端硬盘中。
引用文献:
- Files of Drive API
- webContentLink: A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.
- getFileById(id)
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
关于google-apps-script - 如何解析存储在我的谷歌驱动器中但作为 html 类型突出的 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279456/