1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import scala.xml.{NodeSeq, XML}

class DynamicXML(n: NodeSeq) extends Dynamic {
  def _select_(name: String) = {
    new DynamicXML(n \ name)
  }
  def _invoke_(name: String)(args: Any*) = {
    error("unsupported")
  }
  override def typed[T] = n.asInstanceOf[T]
  override def toString = "DynamicXML(" + n.toString + ")"
}

// A string, to prove I'm not cheating!
val s: String = ("""
  <book>
    <author>
      <firstName>William</firstName>
      <lastName>Shakespeare</lastName>
    </author>
    <title>Hamlet</title>
  </book>""")

val book = new DynamicXML(XML.loadString(s))

book.author.lastName
book.title.typed[NodeSeq].text