Scala: XML Attribute parsing

后端 未结 4 1404
误落风尘
误落风尘 2021-01-02 02:02

I\'m trying to parse a rss feed that looks like this for the attribute \"date\":



    
        

        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 02:35

    Think about using sequence comprehensions, too. They're useful for dealing with XML, particularly if you need complicated conditions.

    For the simple case:

    for {
      c <- rssFeed \\ "@date"
    } yield c
    

    Gives you the date attribute from everything in rssFeed.

    But if you want something more complex:

    val rssFeed = 
                    
                      
                        
                        
                        
                      
                    
                  
    
    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:

        
                            
                              
                              
                              
                            
                          
        ----
        
                              
                              
                              
                            
        ----
        
        ----
        AA
    

提交回复
热议问题