Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 462389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:04:11+00:00 2026-05-12T23:04:11+00:00

I have a Scala representation of some XML (i.e. a scala.xml.Elem ), and I’d

  • 0

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)))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:04:11+00:00Added an answer on May 12, 2026 at 11:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where I would like to have mixed Java and Scala
I have some Scala code that makes fairly heavy use of generics, and I
I have two scala.xml.Elem objects (actual, expected). I am using JUnit 4, but have
I have Scala 2.8.0 installed and don't wish to use Scala 2.7. Whenever I
I have read that Scala's type system is weakened by Java interoperability and therefore
So I have a Map in Scala like this: val m = Map[String, String](
I have a scala object defined like the following: package com.example object Foo {
We have scalar functions in our database for returning things like number of tasks
I have a small text file that I'd like to read into a scalar
I have worked a bit with Django and I quite like its project/applications model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.