Manoj
Manoj

Reputation: 3997

How to convert Option[scala.xml.Elem] values into String

I am newbie to Play Framework(Scala)in my project I need to convert XML response values as a normal String

What I actually need

val result:Option[Elem] = //response from web server

for eg consider this XML values as result variable value

<response><play>Scala</play><version>2.3.9</version></response>

I need to get the XML values as String like this below

println("resultString:="+resultString)

needed output

resultString:=<response><play>Scala</play><version>2.3.9</version><response>

I check with these below two methods but it didn't convert the whole XML values into String like what I need.It gave only the values like Scala2.3.9 not like XML String.

val resultString:String = result.get.text
val resultString:String = result.get.toString()

Edited

while print the result.get.toString() it prints the XML values as String but what I am doing in my project is I am setting the String value into some JsObject like this Json.obj("resultString"->result.get.toString()). when I get the JsObject from response,it is just showing the values only,not showing the Tags

Upvotes: 0

Views: 2328

Answers (1)

Jus12
Jus12

Reputation: 18034

By default, Scala has the behavior you desire. See the below output:

scala> val resultString = <response><play>Scala</play><version>2.3.9</version></response>
resultString: scala.xml.Elem = <response><play>Scala</play><version>2.3.9</version></response>

scala> println("resultString:="+resultString)
resultString:=<response><play>Scala</play><version>2.3.9</version></response>

This should convert to string:

scala> resultString.toString
res1: String = <response><play>Scala</play><version>2.3.9</version></response>

Upvotes: 2

Related Questions