How to return characters escaped by XML (XML unescape)?

I would like the Scala function to return a String &when setting input &, similarly for all other XML escaped characters.

I tried to use xml.Unparsed, possibly incorrectly, this does not give me the desired result:

scala> val amp = '&'
amp: Char = &

scala> <a>{amp}</a>.toString
res0: String = <a>&amp;</a>

scala> import scala.xml._
import scala.xml._

scala> <a>{amp}</a>.child(0)
res1: scala.xml.Node = &amp;

scala> xml.Unparsed(<a>{amp}</a>.child(0).toString)
res2: scala.xml.Unparsed = &amp;

I also tried using xml.Utility.unescape, but it does not give any output at all:

scala> val sb = new StringBuilder
sb: StringBuilder = 

scala> xml.Utility.unescape("&amp;", sb)
res0: StringBuilder = null

scala> sb.toString
res1: String = ""

scala> 
+5
source share
2 answers

If you just want to get unescaped strings from XML objects, textis your friend:

scala> val el = <a>{amp}</a>
el: scala.xml.Elem = <a>&amp;</a>
scala> el.child(0)
res4: scala.xml.Node = &amp;
scala> el.child(0).text
res5: String = &

scala.xml.EntityRef. , , , , ( Java SAX), "&amp;" EntityRef , , , text.

+6

scala.xml.Utility... :

def unescape(text: String): String = {
  def recUnescape(textList: List[Char], acc: String, escapeFlag: Boolean): String = {
    textList match {
      case Nil => acc
      case '&' :: tail => recUnescape(tail, acc, true)
      case ';' :: tail if (escapeFlag) => recUnescape(tail, acc, false)
      case 'a' :: 'm' :: 'p' :: tail if (escapeFlag) => recUnescape(tail, acc + "&", true)
      case 'q' :: 'u' :: 'o' :: 't' :: tail if (escapeFlag) => recUnescape(tail, acc + "\"", true)
      case 'l' :: 't' :: tail if (escapeFlag) => recUnescape(tail, acc + "<", true)
      case 'g' :: 't' :: tail if (escapeFlag) => recUnescape(tail, acc + ">", true)
      case x :: tail => recUnescape(tail, acc + x, true)
      case _ => acc
    }
  }
  recUnescape(text.toList, "", false)
}
0

All Articles