/* * Copyright (C) 2007-2008 Artima, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Automatically generated Scala interpreter transcript from: * * Programming in Scala (First Edition, Version 6) * by Martin Odersky, Lex Spoon, Bill Venners * * http://booksites.artima.com/programming_in_scala */ scala> import scala.xml._ import scala.xml._ scala> This is some XML. Here is a tag: res0: scala.xml.Elem = This is some XML. Here is a tag: scala> {"hello"+", world"} res1: scala.xml.Elem = hello, world scala> val yearMade = 1955 yearMade: Int = 1955 scala> { if (yearMade < 2000) {yearMade} else xml.NodeSeq.Empty } res2: scala.xml.Elem = 1955 scala> {3 + 4} res3: scala.xml.Elem = 7 scala> {"potential security hole"} res4: scala.xml.Elem = </a>potential security hole<a> scala> "" + "potential security hole" + "" res5: java.lang.String = potential security hole scala> abstract class CCTherm { val description: String val yearMade: Int val dateObtained: String val bookPrice: Int // in US cents val purchasePrice: Int // in US cents val condition: Int // 1 to 10 override def toString = description def toXML = {description} {yearMade} {dateObtained} {bookPrice} {purchasePrice} {condition} } defined class CCTherm scala> val therm = new CCTherm { val description = "hot dog #5" val yearMade = 1952 val dateObtained = "March 14, 2006" val bookPrice = 2199 val purchasePrice = 500 val condition = 9 } therm: CCTherm = hot dog #5 scala> therm.toXML res6: scala.xml.Elem = hot dog #5 1952 March 14, 2006 2199 500 9 scala> {{{{brace yourself!}}}} res7: scala.xml.Elem = {{brace yourself!}} scala> Sounds good.text res8: String = Sounds good scala> input ---> output .text res9: String = input ---> output scala> hello \ "b" res10: scala.xml.NodeSeq = hello scala> hello \ "c" res11: scala.xml.NodeSeq = scala> hello \\ "c" res12: scala.xml.NodeSeq = hello scala> hello \ "a" res13: scala.xml.NodeSeq = scala> hello \\ "a" res14: scala.xml.NodeSeq = hello scala> val joe = joe: scala.xml.Elem = scala> joe \ "@name" res15: scala.xml.NodeSeq = Joe scala> joe \ "@serial" res16: scala.xml.NodeSeq = 123 scala> 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 } fromXML: (scala.xml.Node)CCTherm scala> val node = therm.toXML node: scala.xml.Elem = hot dog #5 1952 March 14, 2006 2199 500 9 scala> fromXML(node) res17: CCTherm = hot dog #5 scala> 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." } proc: (scala.xml.Node)String scala> proc(apple) res18: String = It's an a: apple scala> proc(banana) res19: String = It's a b: banana scala> proc(cherry) res20: String = It's something else. scala> proc(a red apple) res21: String = It's something else. scala> proc() res22: String = It's something else. scala> 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." } proc: (scala.xml.Node)String scala> proc(a red apple) res23: String = It's an a: ArrayBuffer(a , red, apple) scala> proc() res24: String = It's an a: Array() scala> val catalog = hot dog #5 1952 March 14, 2006 2199 500 9 Sprite Boy 1964 April 28, 2003 1695 595 5 catalog: scala.xml.Elem = hot dog #5 1952 March 14, 2006 2199 500 9 ... scala> catalog match { case {therms @ _*} => for (therm <- therms) println("processing: "+ (therm \ "description").text) } processing: processing: hot dog #5 processing: processing: Sprite Boy processing: scala> catalog match { case {therms @ _*} => for (therm @ {_*} <- therms) println("processing: "+ (therm \ "description").text) } processing: hot dog #5 processing: Sprite Boy