在scala中,是否有一种简单的方法可以将XML文档的所有元素表示为列表?
我的代码:
object HelloWorld {
def main(args: Array[String]) {
val res="76561193756669631"
val url=("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXYYYZZZ&steamids="+res+"&format=xml")
val str = scala.io.Source.fromURL(url.toString,"utf-8").mkString
val x=xml.XML.loadString(str)
var r=(x\\"steamid").mkString
r=r.replaceAll("""<[a-zA-Z0-9/]*?>""","")
println(r)
}}
检索xml:
<response>
<players>
<player>
<steamid>76561193756669631</steamid>
<communityvisibilitystate>3</communityvisibilitystate>
<personaname>*******je01</personaname>
<lastlogoff>1320236285</lastlogoff>
<profileurl>
http://steamcommunity.com/profiles/76561193756669631/
</profileurl>
<avatar>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg
</avatar>
<avatarmedium>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg
</avatarmedium>
<avatarfull>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg
</avatarfull>
<personastate>0</personastate>
<primaryclanid>103582791429521408</primaryclanid>
<timecreated>1320224696</timecreated>
<personastateflags>0</personastateflags>
</player>
</players>
</response>
并输出steamid因为我硬编码了。有没有办法输出所有元素及其值的列表?
我的目标:
steamid: 76561193756669631
communityvisibilitystate: 3
personaname: *******je01
....
答案 0 :(得分:1)
在这里你需要做些什么才能获得这些元素:
import scala.xml._
val x = <response>
<players>
<player>
<steamid>76561193756669631</steamid>
<communityvisibilitystate>3</communityvisibilitystate>
<personaname>*******je01</personaname>
<lastlogoff>1320236285</lastlogoff>
<profileurl>
http://steamcommunity.com/profiles/76561193756669631/
</profileurl>
<avatar>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg
</avatar>
<avatarmedium>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg
</avatarmedium>
<avatarfull>
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg
</avatarfull>
<personastate>0</personastate>
<primaryclanid>103582791429521408</primaryclanid>
<timecreated>1320224696</timecreated>
<personastateflags>0</personastateflags>
</player>
</players>
</response>
val allNodes = x \\ "response" \\ "players" \\ "player" flatMap(_.child)
allNodes.foreach(n => println(s"label: ${n.label} -> text: ${n.text}"))
首先找到所有玩家然后获得所需的属性。你会得到以下内容(有些没有任何文字):
label: #PCDATA -> text:
label: steamid -> text: 76561193756669631
label: #PCDATA -> text:
label: communityvisibilitystate -> text: 3
label: #PCDATA -> text:
label: personaname -> text: *******je01
label: #PCDATA -> text:
label: lastlogoff -> text: 1320236285
label: #PCDATA -> text:
label: profileurl -> text:
http://steamcommunity.com/profiles/76561193756669631/
label: #PCDATA -> text:
label: avatar -> text:
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg
label: #PCDATA -> text:
label: avatarmedium -> text:
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg
label: #PCDATA -> text:
label: avatarfull -> text:
https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg
label: #PCDATA -> text:
label: personastate -> text: 0
label: #PCDATA -> text:
label: primaryclanid -> text: 103582791429521408
label: #PCDATA -> text:
label: timecreated -> text: 1320224696
label: #PCDATA -> text:
label: personastateflags -> text: 0
label: #PCDATA -> text: