Skip to content

Instantly share code, notes, and snippets.

@hoegh
Created February 11, 2009 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoegh/62027 to your computer and use it in GitHub Desktop.
Save hoegh/62027 to your computer and use it in GitHub Desktop.
A quick experiment with Scala and Xml
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