scala-xml icon indicating copy to clipboard operation
scala-xml copied to clipboard

Scala.js cannot load XML

Open darkfrog26 opened this issue 7 years ago • 14 comments

I'm attempting to parse XML from a String in Scala.js and I get this compile-time error:

[error] Referring to non-existent class javax.xml.parsers.SAXParserFactory$
[error]   called from scala.xml.factory.XMLLoader.parser()javax.xml.parsers.SAXParser
[error]   called from scala.xml.XML$.parser()javax.xml.parsers.SAXParser
[error]   called from scala.xml.factory.XMLLoader.loadString(java.lang.String)scala.xml.Node
[error]   called from scala.xml.XML$.loadString(java.lang.String)scala.xml.Node

darkfrog26 avatar Mar 09 '18 20:03 darkfrog26

The Java parser that the scala-xml library depends on isn't available from Scala.js at run time.

Note that if it's a literal string you're trying to parse, you should be able to use Scala's XML syntax to just declare your XML literal.

ashawley avatar Mar 10 '18 15:03 ashawley

is this a feature an interested contributor could add...? (by substituting some JS thing for SAX?)

SethTisue avatar Mar 11 '18 17:03 SethTisue

@ashawley and if it's not a String literal?

darkfrog26 avatar Mar 11 '18 17:03 darkfrog26

fwiw, at https://github.com/scala/scala-xml/pull/109#issuecomment-250732641 , @OlivierBlanvillain suggested using fastparse (https://github.com/lihaoyi/fastparse/blob/master/scalaparse/shared/src/main/scala/scalaparse/Xml.scala)

SethTisue avatar Mar 11 '18 18:03 SethTisue

also relevant: #194, which suggests adding getting-started doc for Scala.js that would presumably cover this

SethTisue avatar Mar 11 '18 18:03 SethTisue

I don't mind using scalaparse.Xml, but honestly have no idea how to actually use it in this context.

darkfrog26 avatar Mar 12 '18 13:03 darkfrog26

Perhaps, DOMParser could be used to handle XML strings in Javascript-land? We'd have to write a new "factory" to translate the Document to scala-xml objects.

ashawley avatar Mar 13 '18 02:03 ashawley

Outside of scala.xml.factory.XMLLoader, the only other way to parse XML strings is the scala.xml.parsing.ConstructingParser. It's a little clunky, so I'm less than inclined to recommend it. It also has tiny bit of Java in it, so just enough to make Scala.js barf.

ashawley avatar May 25 '18 14:05 ashawley

You might try this. (And be the first, I guess, it's tested once in scala.js): https://github.com/hansdejong/xml-parser-scalajs

hansdejong avatar Jan 17 '19 01:01 hansdejong

Coursier does something https://github.com/coursier/coursier/blob/9d0b191f0d97e055384ea94f850b98fb501a8cb1/modules/core/js/src/main/scala/coursier/core/compatibility/package.scala, but I suspect it's not aware of the XML namespace - https://github.com/coursier/coursier/issues/1821

eed3si9n avatar Aug 07 '20 04:08 eed3si9n

I am working on doing this, with scalajs-dom, but running into a few issues. Just thought I would mention it here, opening separate issues,

odenzo avatar Apr 21 '22 05:04 odenzo

👍 I understand that @odenzo's solution uses scala-js-dom. This would work in browsers only (not Node.js), so IMO it would make most sense as a separate module within this repo. scala-js-dom is very nearly a stdlib for the Scala.js ecosystem, and I can vouch for its stability as a maintainer, so I think it would be an acceptable dependency to take on here.

Edit: btw, I'd be happy to review such a PR.

armanbilge avatar Apr 22 '22 04:04 armanbilge

Also see xmls: https://index.scala-lang.org/flowtick/xmls/xmls/0.1.11?target=_2.13

phdoerfler avatar Apr 22 '22 07:04 phdoerfler

FYI fs2-data has just released an integration with scala-xml including a pure Scala, streaming XML parser. Therefore it is fully supported on JVM and JS!

  • https://github.com/satabin/fs2-data/releases/tag/v1.4.0
  • https://index.scala-lang.org/satabin/fs2-data/artifacts/fs2-data-xml-scala

armanbilge avatar Jun 10 '22 21:06 armanbilge