philcali edited this page Jul 5, 2012 · 5 revisions

This section covers converting a collection of LMXML nodes into something useful.

The core library ships with two types of converters:

  1. XmlConvert
  2. Transform (a special converter covered in Templating)

Under the Hood

An LmxmlConvert[A] trait is a function object, where type A is the result of the conversion.

Take a look at the XmlConvert definition.

Here is an example of parsing lmxml from a file, using the DefaultLmxml object.

import lmxml._

val xml = DefaultLmxml.fromFile("test.lmxml")(XmlConvert)


Working with Composition

As mentioned earlier, LmxmlConvert objects are simply Scala functions underneath, and therefore, can compose.

As an example, I want to censor all TextNode's coming in, replacing them with REDACTED.

object Censor extends SinglePass[ParsedNode] {
  def single(node: ParsedNode) = node match {
    case t: TextNode => TextNode("REDACTED", children = apply(cs))
    case l: LmxmlNode => l.copy(children = apply(cs))
    case _ => node

val contents = """
  has "many"
  great "features"

val result = DefaultLmxml.convert(contents)(Censor andThen XmlConvert)