loicdescotte
5/16/2014 - 8:36 AM

Simple Scala XML Parsing

Simple Scala XML Parsing

//val xmlNode = xml.XML.loadFile("xxx")

val xmlNode =
<persons>
	<person age="30">
		<name>bob</name>
	</person>
	<person age="40">
		<name>joe</name>
	</person>
</persons>

val personNodes = xmlNode \\ "person"

personNodes.map(personNode => personNode \ "name")
//List(NodeSeq(<name>bob</name>), NodeSeq(<name>joe</name>))

personNodes.flatMap(personNode => (personNode \ "name").map(nameNode => nameNode.text))
//List(bob, joe)

personNodes.map(personNode => personNode \ "@age")
//List(30, 40)