Created
February 11, 2009 14:26
-
-
Save hoegh/62027 to your computer and use it in GitHub Desktop.
A quick experiment with Scala and Xml
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
import scala.xml.{NodeSeq,Text,Elem} | |
def prettyXml( prefix: String, nodes: NodeSeq ): String = { | |
nodes match { | |
case txt : Text => | |
val trimtxt = txt.text.trim | |
if (trimtxt.length > 0) prefix+trimtxt+"\n" else "" | |
case elem : Elem => | |
prefix+"<"+elem.label+">\n"+ | |
prettyXml(prefix+" ", elem.child)+ | |
prefix+"</"+elem.label+">\n" | |
case nodes : NodeSeq => | |
("" /: nodes) (_ + prettyXml(prefix, _)) | |
} | |
} | |
println( prettyXml("", <test> | |
<tag>1</tag> | |
<tag>2<subtag>a</subtag>3</tag> | |
<group> | |
<subgroup> | |
<element>I</element> | |
<rank>A</rank> | |
</subgroup> | |
</group> | |
</test>) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment