Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Convert org.w3c.dom.Document to scala.xml.NodeSeq

Tags:

java

dom

xml

scala

Title is pretty self-explanatory. How can I convert an instance of org.w3c.dom.Document to a Scala NodeSeq, to enjoy it's facilitation?

Cheers
Parsa

like image 902
parsa Avatar asked Oct 13 '10 09:10

parsa


3 Answers

  def asXml(dom: org.w3c.dom.Node): Node = {
    val dom2sax = new DOM2SAX(dom)
    val adapter = new NoBindingFactoryAdapter
    dom2sax.setContentHandler(adapter)
    dom2sax.parse()
    return adapter.rootElem
  }
like image 176
IttayD Avatar answered Oct 30 '22 06:10

IttayD


IttayD's answer is good for all w3c XMLs - except dom4j w3c compatible xmls. the following works for all w3c types:

def asXml(dom: _root_.org.w3c.dom.Node): Node = {
    val source = new DOMSource(dom)
    val adapter = new NoBindingFactoryAdapter
    val saxResult = new SAXResult(adapter)
    val transformerFactory = javax.xml.transform.TransformerFactory.newInstance()
    val transformer = transformerFactory.newTransformer()
    transformer.transform(source, saxResult)
    adapter.rootElem
  }
like image 5
nir laor Avatar answered Oct 30 '22 07:10

nir laor


I wrote this code a while back to go in the other direction, from a Scala node to a Dom4J Node. It shows the basic idea of recursing over a tree and should be easy enough to adapt:

implicit def scalaToDom4j(n : Node) : DElem = {

  def inner(n : Node) : Option[DNode] = {
    n match {
      case e : Elem =>
        val elem = DocumentHelper.createElement(e.label)
        for(c <- e.child) yield inner(c) collect {
          case Some(child) => elem.add(child)
        }
        Some(elem)
        //as Scala's xml is type-aware, text might not actually be a Text node,
        //but an Atom of some other type
      case t : Atom[_] =>
        Some(DocumentHelper.createText(t.data.toString))
      case x => None
    }
  }

  //Attempt the conversion. Throw an exception if something has gone badly wrong
  //inner returns an Option[DNode], but the expected top-level type is a DElem
  // (which is a subclass of DNode)
  //so we also validate this.
  inner(trim(n)) map (_.asInstanceOf[DElem]) getOrElse (error("xml failed"))
}
like image 3
Kevin Wright Avatar answered Oct 30 '22 07:10

Kevin Wright