Scala学习笔记8 (xml)

8.  XML

8.1.     生成

Scala原生支持xml,就如同Java支持String一样,这就让生成xmlxhtml很简单优雅:

val name = "james"

val age = 10

val html = name={name}, age="{age}" toString

// name=james, age="10"

又如:

val html = <span>{myTitle}</span>{"hello world"}

 

更复杂的例子:

val x = {(1 to 5).map(i => {i})}

// 12345

 

val x0 =

val {u} = x0  // u: scala.xml.Node = 

 

By the way, if you want to include a curly brace (`{' or `}') as XML text, as opposed to using them to escape to Scala code, simply write two curly braces in a row:

  scala>  {{{{brace yourself!}}}} 
  res1: scala.xml.Elem =  {{brace yourself!}} 


8.2.       xml文件

xml.XML loadString "

"

xml.XML loadFile "abc.xml"

 

xml.XML.saveFull("foo.xml", node, "UTF-8", xmlDecl: Boolean, doctype : DocType)


8.3.      读取:

val x = {(1 to 5).map(i => {i})}

// 12345

(x \ "e") map (_.text.toInt) // List(1, 2, 3, 4, 5)

 

val x0 =

20

30

(x0 \ "user") // 20, 30)

(x0 \ "user" \ "age") // (20, 30)

(x0 \ "age")  // 直接下级: ()

(x0 \\ "age") // 所有下级:(20, 30)

(x0 \ "_") 所有


8.4       访问属性

val x = <u name="qh" />

scala> (x \ "u" \\ "@name") foreach println
qh
james
qiu

 

例子:

val data =


 
 


val res = for (item <- data \ "item" ; 
                 price = (item \ "@price").text.toDouble ; 
                 qty = (item \ "@quantity").text.toInt)
           yield (price * qty)

printf("$%.2f\n", res.sum)

8.5       Deserialization

You can write of a serializer, a parser from XML back into your internal data structures. For example, you can parse back a CCTherm instance by using the following code:

def fromXML(node: scala.xml.Node): CCTherm =
    new CCTherm {
        val description = (node \ "description").text
        val yearMade = (node \ "yearMade").text.toInt
        val dateObtained = (node \ "dateObtained").text
        val bookPrice = (node \ "bookPrice").text.toInt
        val purchasePrice = (node \ "purchasePrice").text.toInt
        val condition = (node \ "condition").text.toInt
}


This code searches through an input XML node, named node, to find each of the six pieces of data needed to specify a CCTherm. The data that is text is extracted with .text and left as is.


8.6     格式化输出

val pp = new xml.PrettyPrinter(80, 4)  // 行宽 80,缩进为 4  

pp formatNodes   

结果是字符串 

 

     

 

 

8.7     Pattern matching on XML

A pattern embedded in {} can use the full Scala pattern language, including binding new variables, performing type tests, and ignoring content using the _ and _* patterns. Here is a simple example:
def proc(node: scala.xml.Node): String =
node match {
case {contents} => "It's an a: "+ contents
case {contents} => "It's a b: "+ contents
case _ => "It's something else."
}

scala> proc(apple)
res16: String = It's an a: apple
scala> proc(banana)
res17: String = It's a b: banana

scala> proc(cherry)
res18: String = It's something else.

val catalog =


 
    hot dog #5
    1952
    March 14, 2006
    2199
    500
    9
 

 
    Sprite Boy
    1964
    April 28, 2003
    1695
    595
    5
 

catalog match {
  case {therms @ _*} =>
    for (therm @ {_*}       println("processing: "+(therm \ "description").text)
}


processing: hot dog #5
processing: Sprite Boy

你可能感兴趣的:(Scala)