使用Scala XML从XML检索href / url数据时出现问题

时间:2018-08-05 21:22:21

标签: xml scala rss

示例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 feed获取各种数据。

例如,获取有关Reddit帖子标题的信息。以下代码是:

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

1 个答案:

答案 0 :(得分:2)

这很好:

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")

}