Skip to content

Instantly share code, notes, and snippets.

@huynhjl
Created January 23, 2010 21:36
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 huynhjl/284811 to your computer and use it in GitHub Desktop.
Save huynhjl/284811 to your computer and use it in GitHub Desktop.
import scala.xml.pull._
import scala.xml.UnprefixedAttribute
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.Stack
import scala.io.Source
class Joint(name:String) {
val joints = ListBuffer[Joint]()
def addJoint(joint:Joint) {
joints += joint
}
override def toString:String = {
"joint_" + name + ": " + joints.mkString("[", ",", "]")
}
}
val source = Source.fromString("""<model>
<joint name="pelvis">
<joint name="lleg">
<joint name="lfoot"/>
</joint>
<joint name="rleg">
<joint name="rfoot"/>
</joint>
</joint>
</model>""")
var result: Joint = null
val xer = new XMLEventReader(source)
val ancestors = new Stack[Joint]()
while (xer.hasNext) {
xer.next match {
case EvElemStart(_, "joint", UnprefixedAttribute(_, name, _), _) =>
val joint = new Joint(name.toString)
if (ancestors.nonEmpty)
ancestors.top.addJoint(joint)
ancestors.push(joint)
case EvElemEnd(_, "joint") =>
result = ancestors.pop
case _ =>
}
}
println(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment