Skip to content
giabao edited this page Oct 4, 2013 · 4 revisions

Configure your project to use xmlsoap-ersatz in project/Build.scala

    object ApplicationBuild extends Build {

      val appName         = "play2-xmlsoap"
      val appVersion      = "1.0-SNAPSHOT"

      val appDependencies = Seq(
        "play2.tools.xml" %% "xmlsoap-ersatz" % "0.1-SNAPSHOT"
      )  

      val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
        resolvers += ("mandubian-mvn snapshots" at "https://github.com/mandubian/mandubian-mvn/raw/master/snapshots")
      )
    }

A very simple sample of xmlsoap-ersatz in a Play2 controller

    package controllers

    import play.api._
    import play.api.mvc._
    import play2.tools.xml._
    import play2.tools.xml.DefaultImplicits._

    object Application extends Controller {
       case class Foo(id: Long, name: String, age: Option[Int])

       implicit object FooXMLF extends XMLFormatter[Foo] {
          def read(x: xml.NodeSeq): Option[Foo] = {
    	    for( 
              id <- EXML.fromXML[Long](x \ "id");
      	      name <- EXML.fromXML[String](x \ "name");
              age <- EXML.fromXML[Option[Int]](x \ "age")
            ) yield(Foo(id, name, age))
          }

          def write(f: Foo, base: xml.NodeSeq): xml.NodeSeq = {
            <foo>
              <id>{ f.id }</id>
              <name>{ f.name }</name>
              { EXML.toXML(f.age, <age/>) }
            </foo>
         }
      }  

      def index = Action {
         Ok("coucou")
      }

      def foo = Action(parse.xml) { request =>
        EXML.fromXML[Foo](request.body).map { foo =>
          Ok(EXML.toXML(foo))
        }.getOrElse{
          BadRequest("Expecting Foo XML data")
        }
      }
  
    }