I have a Scala representation of some XML (i.e. a scala.xml.Elem
), and I'd like to use it with some of the standard Java XML APIs (specifically SchemaFactory). It looks like converting my Elem
to a javax.xml.transform.Source
is what I need to do, but I'm not sure. I can see various ways to effectively write out my Elem
and read it into something compatible with Java, but I'm wondering if there's a more elegant (and hopefully more efficient) approach?
Scala code:
import java.io.StringReader
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.{Schema, SchemaFactory}
import javax.xml.XMLConstants
val schemaXml = <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="foo"/>
</xsd:schema>
val schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// not possible, but what I want:
// val schema = schemaFactory.newSchema(schemaXml)
// what I'm actually doing at present (ugly)
val schema = schemaFactory.newSchema(new StreamSource(new StringReader(schemaXml.toString)))