你想要的是
-您只需轻轻地告诉Scala编译器如何从
scala.xml.Elem
到
javax.xml.transform.stream.StreamSource
.
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)
它的效率并没有提高,但一旦你得到了隐式方法定义,它肯定会更漂亮。