I had to solve this problem in my application. Here is some code based on your example case class that you might find to be helpful.
My approach uses Unmarshaller.delegate
as discussed here.
import scala.xml.Node
import scala.xml.NodeSeq
import spray.httpx.unmarshalling._
import spray.httpx.unmarshalling.Unmarshaller._
case class Person(name: String, age: Int)
object Person {
def fromXml(node: Node): Person = {
// add code here to instantiate a Person from a Node
}
}
case class PersonSeq(persons: Seq[Person])
object PersonSeq {
implicit val PersonSeqUnmarshaller: Unmarshaller[PersonSeq] = Unmarshaller.delegate[NodeSeq, PersonSeq](MediaTypes.`text/xml`, MediaTypes.`application/xml`) {
// Obviously, you'll need to change this function, but it should
// give you an idea of how to proceed.
nodeSeq =>
val persons: NodeSeq = nodeSeq \ "PersonList" \ "Person"
PersonSeq(persons.map(node => Person.fromXml(node))
}
}