Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Scala/XML

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Scala makes processing and producing XML very easy. This enables programmers to very easily talk to web services and other sources of XML data. XML Literals In Scala, XML can be used as a literal. This means you can do something like this: val doc = <person living="true"><name>Jimbo Wales</name><job>Supreme Overlord of Wikiland</job></person> You can include expressions within XML using curly braces like this: class Person { // ... def toXML() = <person><name>{this.name}</name><job>{this.job}</job></person> } As you can see curly braces are special characters in Scala XML, therefore if you just want to display a curly brace just repeat it twice: {{ Exploring XML When you have some XML, you can explore it using the backslash and double backslash methods: val doc = <person living="true"><name>Jimbo Wales</name><job>Supreme Overlord of Wikiland</job></person> println((doc \ "name").text) // prints "Jimbo Wales" println(doc.attributes("living")) // prints "true" println(doc \\ "@living") // finds all the attributes named living and in this case prints true