Scala:XML 属性解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2851365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: XML Attribute parsing
提问by Chris
I'm trying to parse a rss feed that looks like this for the attribute "date":
我正在尝试为属性“日期”解析如下所示的 rss 提要:
<rss version="2.0">
<channel>
<item>
<y:c date="AA"></y:c>
</item>
</channel>
</rss>
I tried several different versions of this: (rssFeed contains the RSS data)
我尝试了几个不同的版本:(rssFeed 包含 RSS 数据)
println(((rssFeed \ "channel" \ "item" \ "y:c" \"date").toString))
But nothing seems to work. What am I missing?
但似乎没有任何效果。我错过了什么?
Any help would really be appreciated!
任何帮助将不胜感激!
回答by sblundy
The "y" in <y:cis a namespace prefix. It's not part of the name. Also, attributes are referred to with a '@'. Try this:
中的“y”<y:c是命名空间前缀。它不是名称的一部分。此外,属性用“@”表示。试试这个:
println(((rssFeed \ "channel" \ "item" \ "c" \ "@date").toString))
回答by Daniel Spiewak
Attributes are retrieved using the "@attrName" selector. Thus, your selector should actually be something like the following:
使用“@attrName”选择器检索属性。因此,您的选择器实际上应该类似于以下内容:
println((rssFeed \ "channel" \ "item" \ "c" \ "@date").text)
回答by James Moore
Also, think about the difference between \and \\. \\looks for a descendent, not just a child, like this (note that it jumps from channel to c, without item):
另外,想想\和\\之间的区别。 \\寻找一个后代,而不仅仅是一个孩子,像这样(注意它从通道跳转到 c,没有项目):
scala> (rssFeed \ "channel" \ "c" \ "@date").text
res20: String = AA
Or this sort of thing if you just want all the < c > elements, and don't care about their parents:
或者这样的事情,如果你只想要所有的 < c > 元素,而不关心他们的父母:
scala> (rssFeed \ "c" \ "@date").text
res24: String = AA
And this specifies an exact path:
这指定了一个确切的路径:
scala> (rssFeed \ "channel" \ "item" \ "c" \ "@date").text
res25: String = AA
回答by James Moore
Think about using sequence comprehensions, too. They're useful for dealing with XML, particularly if you need complicated conditions.
也可以考虑使用序列推导式。它们对于处理 XML 很有用,特别是在您需要复杂条件时。
For the simple case:
对于简单的情况:
for {
c <- rssFeed \ "@date"
} yield c
Gives you the date attribute from everything in rssFeed.
为您提供 rssFeed 中所有内容的日期属性。
But if you want something more complex:
但如果你想要更复杂的东西:
val rssFeed = <rss version="2.0">
<channel>
<item>
<y:c date="AA"></y:c>
<y:c date="AB"></y:c>
<y:c date="AC"></y:c>
</item>
</channel>
</rss>
val sep = "\n----\n"
for {
channel <- rssFeed \ "channel"
item <- channel \ "item"
y <- item \ "c"
date <- y \ "@date" if (date text).equals("AA")
} yield {
val s = List(channel, item, y, date).mkString(sep)
println(s)
}
Gives you:
给你:
<channel>
<item>
<y:c date="AA"></y:c>
<y:c date="AB"></y:c>
<y:c date="AC"></y:c>
</item>
</channel>
----
<item>
<y:c date="AA"></y:c>
<y:c date="AB"></y:c>
<y:c date="AC"></y:c>
</item>
----
<y:c date="AA"></y:c>
----
AA

