简体   繁体   English

Google App脚本中的XML与XMLService类

[英]XML vs XMLService class in Google App Script

I got help from a question answer . 我从问答中得到了帮助。 But it uses XML.parse() . 但它使用XML.parse() Yet I cannot find XML class in GAS, there is only XMLService class. 但是我在GAS中找不到XML类,只有XMLService类。

Would you explain the difference if any? 你会解释其中的差异吗? Is XML class outdated? XML类过时了吗?

Update 更新

Spencer mentioned to switch to XMLService: Spencer提到切换到XMLService:

Use the XmlService now 立即使用XmlService

But if I alredy use XML.parse() from the above answer code, will not the change affect the code? 但是,如果我从上面的答案代码中使用XML.parse() ,那么更改是否会影响代码?

XML has depreciated. XML已经贬值了。 Use the XmlService now. 立即使用XmlService。 XmlService.parse() cannot be used as a drop in replacement. XmlService.parse()不能用作替换。

Here is the function from your linked post written with XmlService: 以下是使用XmlService编写的链接帖子中的函数:

function getTextFromHtml(html) {
  var parsed = XmlService.parse(html);
  var dec = parsed.getDescendants();
  var returnText = []
  for(var i in dec){
   if(dec[i].asText() != null){
     returnText.push(dec[i].asText());
   }
  }
   return returnText.join(' ');
}

There are some caveats to this though. 但是有一些警告。 XmlService.parse() does not have a check for HTML parameter. XmlService.parse()没有检查HTML参数。 If your HTML is malformed it will throw an error. 如果您的HTML格式不正确,则会引发错误。 So for the example in the previous answer : 因此,对于上一个答案中的示例:

hello <div>foo</div>&amp; world <br /><div>bar</div>!

needs to be: 需要是:

<html>hello <div>foo</div>&amp; world <br /><div>bar</div>!</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM