How to iterate XML in Request


(sowmya) #1

Hi,

     I am sending xml post request in play framework (scala) in postman  as below
<employees>
	<employee>
		<id>1</id>
		<name>Ram</name>
		<dept>1</dept>
		<doj>1/2/2018</doj>
	</employee>
		<employee>
		<id>2</id>
		<name>Sam</name>
		<dept>2</dept>
		<doj>1/6/2018</doj>
	</employee>
		<employee>
		<id>3</id>
		<name>Tom</name>
		<dept>3</dept>
		<doj>7/8/2018</doj>
	</employee>
</employees>

i want names of all employee like

Hello Ram, Sam and Tom

My code

def sayHello = Action(parse.xml) { request =>
  (request.body \\ "name" headOption).map(_.text).map { name =>
    Ok("Hello " + name)
  }.getOrElse {
    BadRequest("Missing parameter [name]")
  }
}

My output

Hello Ram

But i want Hello Ram, Sam and Tom

So i am not getting how to iterate XML