scala-xml
scala-xml copied to clipboard
Scala.js cannot load XML
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
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.
is this a feature an interested contributor could add...? (by substituting some JS thing for SAX?)
@ashawley and if it's not a String literal?
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)
also relevant: #194, which suggests adding getting-started doc for Scala.js that would presumably cover this
I don't mind using scalaparse.Xml, but honestly have no idea how to actually use it in this context.
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.
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.
You might try this. (And be the first, I guess, it's tested once in scala.js): https://github.com/hansdejong/xml-parser-scalajs
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
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,
👍 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.
Also see xmls: https://index.scala-lang.org/flowtick/xmls/xmls/0.1.11?target=_2.13
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