0

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
Manoj
  • 3,947
  • 9
  • 46
  • 84
  • did you try `result.get.mkString`? – Zoltán Nov 02 '15 at 12:59
  • The tag `` is not closed properly with ``. Other wise `result.get.toString` will work. – Johny T Koshy Nov 02 '15 at 13:01
  • @johny actually I get some `xml` values here I posted `dummy` values.all tags in original response has been closed properly. – Manoj Nov 02 '15 at 13:04
  • To be sure I just tried again in REPL and `toString` works fine for me. – Johny T Koshy Nov 02 '15 at 13:08
  • @johny I check with `result.get.toString()` it is returning the same value what I have mentioned in my question. – Manoj Nov 02 '15 at 13:35
  • @Zoltán I check with `result.get.mkString` it also returning the same. – Manoj Nov 02 '15 at 13:38
  • @johny 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 `JsValue` from response,it is just showing the values only,not showing the `Tags` – Manoj Nov 02 '15 at 13:57
  • @Manoj what does this print: `println(Json.obj("resultString"->result.get.toString()))`? – Jus12 Nov 02 '15 at 14:17
  • http://stackoverflow.com/questions/9516973/xml-to-json-with-scala this should be helpful for you – vvg Nov 02 '15 at 14:21
  • @Jus12 `println(Json.obj("resultString"->result.get.toString()))` prints the XML String values – Manoj Nov 02 '15 at 14:23
  • @Manoj can you paste the actual output. Not very clear what you mean by XML String values. – Jus12 Nov 02 '15 at 15:07
  • `{"resultString":"Scala2.3.9"}` – Manoj Nov 02 '15 at 15:14

1 Answers1

2

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>
Jus12
  • 17,824
  • 28
  • 99
  • 157