Skip to content

Instantly share code, notes, and snippets.

@propensive
Last active February 20, 2018 11:54
Show Gist options
  • Save propensive/75b8dea7c1ceb466c73d to your computer and use it in GitHub Desktop.
Save propensive/75b8dea7c1ceb466c73d to your computer and use it in GitHub Desktop.
Really Basic Rapture XML basic demo
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.xml._
import rapture.xml._
scala> import xmlBackends.stdlib._
import xmlBackends.stdlib._
scala> val x1 = xml"<foo><bar/></foo>"
x1: rapture.xml.Xml = <foo><bar/></foo>
scala> val x2 = xml"<foo><bar></foo>"
<console>:16: error: failed to parse Xml literal: expected 'b', but found 'f'
val x2 = xml"<foo><bar></foo>"
^
scala> val x2 = xml"<foo><bar/></baz>"
<console>:16: error: failed to parse Xml literal: expected 'f', but found 'b'
val x2 = xml"<foo><bar/></baz>"
^
scala> val x2 = xml"<foo><bar/></foo"
<console>:16: error: failed to parse Xml literal: expected '>', but found end of input
val x2 = xml"<foo><bar/></foo"
^
scala> val x2 = xml"""<foo att="quux"><bar/></foo>"""
x2: rapture.xml.Xml = <foo att="quux"><bar/></foo>
scala> x2.bar
res0: rapture.xml.Xml = <bar/>
scala> val x3 = xml"""<x><item id="1"/><item id="2"/><item id="3">three</item></x>"""
x3: rapture.xml.Xml = <x><item id="1"/><item id="2"/><item id="3">three</item></x>
scala> x3.item
res1: rapture.xml.Xml = <item id="1"/><item id="2"/><item id="3">three</item>
scala> x3.item(1)
res2: rapture.xml.Xml = <item id="2"/>
scala> x2.item(2)
res4: rapture.xml.Xml = undefined
scala> x3.item(2).as[String]
res5: String = three
scala> case class Foo(a: Int, b: String, c: Double)
defined class Foo
scala> Xml(Foo(1, "two", 3.0))
<console>:19: warning: method any2ArrowAssoc in trait DeprecatedPredef is deprecated: Use `ArrowAssoc`
Xml(Foo(1, "two", 3.0))
^
res6: rapture.xml.Xml = <a>1</a><b>two</b><c>3.0</c>
scala> case class Bar(foo: Foo, baz: Boolean)
defined class Bar
scala> Xml(Bar(Foo(1, "two", 3.0), true))
<console>:21: warning: method any2ArrowAssoc in trait DeprecatedPredef is deprecated: Use `ArrowAssoc`
Xml(Bar(Foo(1, "two", 3.0), true))
^
res7: rapture.xml.Xml = <foo><a>1</a><b>two</b><c>3.0</c></foo><baz>true</baz>
scala> xml"""<foo>42</foo>"""
res12: rapture.xml.Xml = <foo>42</foo>
scala> res12.as[Int]
<console>:18: error: cannot extract type Int from rapture.xml.Xml.
res12.as[Int]
^
scala> implicit val intExt = Xml.extractor[String].map(_.toInt)
intExt: rapture.data.Extractor[Int,rapture.xml.Xml]{type Throws = rapture.data.DataGetException with Exception} = rapture.data.Extractor$$anon$7@4ef52ca7
scala> res12.as[Int]
res15: Int = 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment