Professional Documents
Culture Documents
1
and maintenance of programs that deal with XML.
It provides a data model for XML by means of traits and
particular subclasses. Processing of XML can then be done
by deconstructing the data using Scala's pattern matching
mechanism.
8.1 Data Model
Scala's data model for XML is an immutable representation
of an ordered unranked tree. In such a tree each node has
a label, a sequence of children nodes, and a map from attribute
keys to attribute values. This is speci_ed in the trait
scala.xml.Node which additionally contains equivalents of
the XPath operators child and descendant-or-self, which are
written \ and \\. Concrete subclasses exist for elements,
text nodes, comments, processing instructions, and entity
references.
XML syntax can be used directly in a Scala program,
e.g., in value de_nitions.
val labPhoneBook =
<phonebook>
<descr>Phone numbers of<b>XML</b> hackers.</descr>
<entry>
<name>Burak</name>
<phone where="work"> +41 21 693 68 67 </phone>
<phone where="mobile"> +41 78 601 54 36 </phone>
</entry>
</phonebook>;
The value labPhoneBook is an XML tree; one of its nodes has
the label phone, a child sequence consisting of a text node
labeled by +41 2.., and a map from the attribute key where
to the value "work". Within XML syntax it is possible to escape
to Scala using the brackets { and } (similar to the convention
used in XQuery). For example, a date node with a
child text node consisting of the current date can be de_ned
by <date>{ df.format(new java.util.Date()) }</date>.