user3809888
user3809888

Reputation: 407

How to strip text from XML string in Scala?

I have an XML string where some nodes have text between the tags, like so:

<note>
<to>Text between tags</to>
<from>More text</from>
<empty />
</note>

In Scala, how can I remove the text between these tags so that I get a string like this?:

<note><to></to><from></from><empty /></note>

Upvotes: 2

Views: 343

Answers (1)

vsnyc
vsnyc

Reputation: 2257

You can use a RewriteRule for this and set Text nodes to Empty e.g.

val removeText = new RewriteRule {
  override def transform(n: Node): NodeSeq = n match  {
    case e: Text => NodeSeq.Empty
    case _ => n
  }
}

Then you can use RuleTransformer to transform your XML e.g.

  val source = scala.io.Source.fromFile("myData.xml")
  val lines = try source.mkString finally source.close()
  val xml = XML.loadString(lines)
  val output = new RuleTransformer(removeText).transform(xml)
  println(output)

Output: <note><to/><from/><empty/></note>

Upvotes: 2

Related Questions