使用 Scala XML 从 XML 检索 href/url 数据的问题

Issue retrieving href/url data from XML using Scala XML

样本XML:

<?xml version="1.0" encoding="UTF-8"?>
<entry>
   <author>
      <name>/u/Kobe_to_Boston</name>
      <uri>https://www.reddit.com/user/Kobe_to_Boston</uri>
   </author>
   <id>t3_94t5in</id>
   <link href="https://www.reddit.com/r/hiphopheads/comments/94q6ks/travis_scott_stop_trying_to_be_god_ft_kid_cudi/" />
   <updated>2018-08-05T16:38:29+00:00</updated>
   <title>The Weeknd - The Hills</title>
</entry>

正在使用Scala XML Library。我正在尝试从 Reddit RSS 提要中获取各种数据。

例如,获取有关 Reddit 标题的信息 post。以下代码为:

val redditPostTitle = (XML.loadString(xmlContent) \ "entry" \ "title").head.text 
//assume xmlContent variable is the contains the XML above

以上作品。

现在,问题是,我想从 "link href" 标签中检索数据。我尝试了各种组合:

val redditPostUrl = (XML.loadString(xmlContent) \ "entry" \ "link href").head.text 

但是我返回了一个空字符串。我想要返回的是:

"https://www.reddit.com/r/hiphopheads/comments/94q6ks/travis_scott_stop_trying_to_be_god_ft_kid_cudi/

已解决:解决方案是:

(XML.loadString(hhhContent) \ "entry" \ "link" \ "@href").text

这很好用:

object Example extends App {

  val feed=
    <entry>
      <author>
        <name>/u/Kobe_to_Boston</name>
        <uri>https://www.reddit.com/user/Kobe_to_Boston</uri>
      </author>
      <id>t3_94t5in</id>
      <link href="https://www.reddit.com/r/hiphopheads/comments/94q6ks/travis_scott_stop_trying_to_be_god_ft_kid_cudi/" />
      <updated>2018-08-05T16:38:29+00:00</updated>
      <title>The Weeknd - The Hills</title>
    </entry>

  println(feed \ "link" \ "@href")

}