Scala XML Match nodes using Attributes -


i loading xml file in scala looks this:

<dataset>   <item label="neutral" target="general" tweetid="936466790" username="b_e_x">     <content>jim lehrer directed debate audience ... 30 seconds ... #tweetdebate</content>   </item>   <item label="neutral" target="general" tweetid="936466992" username="jonathan fields">     <content>here go. #tweetdebate</content>   </item> </dataset> 

now, trying labels of each of items using attributes, returns me none? tried several ways matching, parsing etc.:

val rawxml = xml.loadfile(file).tolist rawxml.foreach(x => println(x.attribute("label"))) 

i tried matching follows:

myxml match {      case <dataset>     {item @ <item>{thetext}</item>}     </dataset> =>      println("an %s text: %s".format(item \ "@label", thetext)) 

there few ways this. problem first version you're not searching child nodes "label":

//note 3 whitespace nodes scala> rawxml.child.foreach(x => println(x.attribute("label"))) none some(neutral) none some(neutral) none 

you can more refined search of children nodes "item"s "label" attributes:

scala> rawxml \ "item" \\ "@label" res0: scala.xml.nodeseq = nodeseq(neutral, neutral) 

if you're doing lot work xml in scala recommend using anti-xml. gives lot better syntax , performance improvements (for part) on scala's native xml handling.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -