如何解析存储在我的Google驱动器中但以html类型突出的XML文件?!
我在Google云端硬盘云上保存了以下来源的xml副本:http://api.allocine.fr/rest/v3/movie?media=mp4-lc&partner=YW5kcm9pZC12Mg&profile=large&version=2&code=265621我可以解析源,但是我无法解析看起来像html类型的copie的xml!我有如下解析错误:元素类型“元”必须由匹配的结束标记“”终止或元素类型“ 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文件”!是对的,这仅仅是意味着我无法实现我的脚本吗?
如果我的理解是正确的,那么这个答案呢?
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。DriveApp.getFileById(fileId).getBlob().getDataAsString()
脚本检索数据。例如,当使用共享的https://drive.google.com/file/d/16kJ5Nko-waVb8s2T12LaTEKaFY01603n/view?usp=sharing
示例文件时,脚本将如下所示。
在此模式下,使用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.
在此模式下,使用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。
- webContentLink:用于使用基于cookie的身份验证在浏览器中下载文件内容的链接。如果内容是公开共享的,则无需任何凭据即可下载内容。
如果我误解了你的问题,而这不是你想要的方向,我深表歉意。
很棒!你写。您的两个建议正在起作用。我只是在代码的其他地方犯了一个错误。因此该解决方案1不再起作用。这就是为什么要提供一个新脚本进行测试的原因。仅出于培训目的,因为我的项目很安全,谢谢您:)
function storeXmlOnGoogleDriveThenParsIt(url){
url=url||"http://api.allocine.fr/rest/v3/movie?media=mp4-lc&partner=YW5kcm9pZC12Mg&profile=large&version=2&code=265621"; // to test
// on my Google Drive i make a copi of the url called. (This to preserve the server from too many request.)
var bufferedXml=DriveApp.getRootFolder().searchFolders('title = "BufferFiles"').next().createFile("xmlBuffered.xml", UrlFetchApp.fetch(url).getContentText(),MimeType.PLAIN_TEXT);
var urlBufferedXml=bufferedXml.getUrl() // The new url ,of the buffered file
var fileId=urlBufferedXml.match(/https:\/\/drive.google.com\/file\/d\/(.*)\/view.*/)[1];
//Now i want to pars the buffered xml file
//[ Your seconde way to get data is working perect ! THANK YOU A LOT !!!
var data = DriveApp.getFileById(fileId).getBlob().getDataAsString();
var xmlDocument=XmlService.parse(data);
var root=xmlDocument.getRootElement();
var mynamespace=root.getNamespace();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log("keywords:"+keywords) // and parsing success ]
//[ The first way to get data was ok BUT DAMNED it now aborting ! Since modifications on the line code that create the xml, and i cant' retrieve the right code
var downloadUrlBufferedXml="https://drive.google.com/uc?id="+fileId+"&export=download";
var data = UrlFetchApp.fetch(downloadUrlBufferedXml).getContentText(); // was good but now data is here again like a html text ! :(
Logger.log("data"+data.substring(1,350)); // this show that data is HTML type and not XML type ! :(
var xmlDocument=XmlService.parse(data); // So i have Error like: The element type "meta" must be terminated by the matching end-tag "</meta>" ]
var root=xmlDocument.getRootElement();
var mynamespace=root.getNamespace();
var keywords=root.getChild("keywords",root.getNamespace()).getText();
Logger.log("keywords:"+keywords)
}