Last active
August 29, 2015 14:01
-
-
Save loicdescotte/70c68e92bb6f499f78ec to your computer and use it in GitHub Desktop.
Simple Scala XML Parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be written :