Can't get element javascript google apps scrip

2019-08-10 01:40发布

I'm trying to get the link element in a feed, i can get the description and title, but cannot get the link element. It seems weird to me. Here is my code

var url = "http://healthyhow.net/feed";
var response = UrlFetchApp.fetch(url);
var shareDoc = Xml.parse(response.getContentText(), true);

var root = shareDoc.getElement(); // feed element

var entries = root.getElement("channel").getElements("item");
for (var i=0; i<1; i++) { //just pick the first entry
    var e = entries[i];
    var title = e.getElement("title").getText();
    var link = e.getElement("link").getText();
   var description = e.getElement("description").getText();
}

Could anyone point out what is wrong here? Thanks!

1条回答
Root(大扎)
2楼-- · 2019-08-10 02:11

The docs indicate you should use lenient parsing for HTML - it's unclear what this is doing underneath, but in your case maybe it's confusing an HTML <link> element with a generic XML element with that same tag name. It appears to parse the link entries into something like this for your code (which you can see in shareDoc.toXmlString()):

<link/>http://healthyhow.net/l-arginine-natural-treatment-for-hypertension/

Since it's an empty tag, no text.

Change:

var shareDoc = Xml.parse(response.getContentText(), true);

to be:

var shareDoc = Xml.parse(response.getContentText(), false);

and you should be able to get the link text.

查看更多
登录 后发表回答