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)))
What you want is possible - you just have to gently tell the Scala compiler how to go from scala.xml.Elem
to javax.xml.transform.stream.StreamSource
by declaring an implicit method.
import java.io.StringReader
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.{Schema, SchemaFactory}
import javax.xml.XMLConstants
import scala.xml.Elem
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);
implicit def toStreamSource(x:Elem) = new StreamSource(new StringReader(x.toString))
// Very possible, possibly still not any good:
val schema = schemaFactory.newSchema(schemaXml)
It isn't any more efficient, but it sure is prettier once you get the implicit method definition out of the way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With