本文介绍了如何将Scala Scales的XML转换为DOM / SAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据这里的示例代码将scale-xml文档转换为DOM文档或SAX对象:

I am trying to transform scales-xml documents into DOM documents or SAX objects based on the example code here: http://scala-scales.googlecode.com/svn/sites/scales/scales-xml_2.9.2/0.4.4/XSLT.html

仅源和往返示例工作正常,但是我写的scales => DOM代码返回一个空文档,而scale => SAX代码引发以下异常:

The source only and roundtrip examples work fine, but the scales=>DOM code I wrote returns an empty document, and the scales=>SAX code raises the following exception:

ERROR:  'org.xml.sax.SAXException: setResult() must be called prior to startDocument().'

我正在努力支持什么?如果是这样,我怎么弄错了?

Is what I am trying to do supported? If so, what did I get wrong?

我的示例代码和完整的错误跟踪如下。

My sample code and full error trace is given below.

感谢您的帮助。

代码

// scales XML parsing
import scales.xml._
import ScalesXml._

object ScalesTransformTest {

  def main(args:Array[String]) {
    val elem = Elem("trax"l)
    val doc = Doc(elem / elem)
    println( s"scales doc:\n${asString(doc)}" )

    import javax.xml.transform._
    import org.w3c.dom.Document
    val tf = TransformerFactory.newInstance
    val trax = tf.newTransformer

    val wr = new java.io.StringWriter
    val str = new stream.StreamResult(wr)
    trax.transform(doc, str)
    println( s"\nsource only:\n${wr.toString}" )

    val sr = ScalesResult()
    trax.transform(doc, sr)
    println( s"\nroundtrip:\n${asString(sr.doc)}" )

    val dr = new dom.DOMResult
    trax.transform(doc, dr)
    val ddoc = dr.getNode.asInstanceOf[Document]
    println( s"\nscales=>DOM:\n$ddoc" )

    val stf = tf.asInstanceOf[sax.SAXTransformerFactory]
    val th = stf.newTransformerHandler
    val strax = th.getTransformer

    val xr = new sax.SAXResult(th)
    strax.transform(doc, xr)
    println( s"\nscales=>SAX:\n$xr" )
  }

}

错误

Picked up JAVA_TOOL_OPTIONS:  -Dfile.encoding=UTF-8 -Xmx4G -XX:MaxPermSize=1G -XX:+CMSClassUnloadingEnabled
[info] [Loading project definition from /Users/eric/git/scala/dgrep/project
[info] [Set current project to DGrep (in build file:/Users/eric/git/scala/dgrep/)
[info] Running ScalesTransformTest 
scales doc:
<?xml version="1.0" encoding="UTF-8"?><trax><trax/></trax>

source only:
<?xml version="1.0" encoding="UTF-8"?><trax><trax/></trax>

roundtrip:
<?xml version="1.0" encoding="UTF-16"?><trax><trax/></trax>

scales=>DOM:
[#document: null]
ERROR:  'org.xml.sax.SAXException: setResult() must be called prior to startDocument().'
[error] (run-main) javax.xml.transform.TransformerException: javax.xml.stream.XMLStreamException: org.xml.sax.SAXException: setResult() must be called prior to startDocument().
javax.xml.transform.TransformerException: javax.xml.stream.XMLStreamException: org.xml.sax.SAXException: setResult() must be called prior to startDocument().
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:739)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
    at ScalesTransformTest$.main(ScalesTransformTest.scala:36)
    at ScalesTransformTest.main(ScalesTransformTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at sbt.Run.invokeMain(Run.scala:72)
    at sbt.Run.run0(Run.scala:65)
    at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
    at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
    at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
Caused by: javax.xml.stream.XMLStreamException: org.xml.sax.SAXException: setResult() must be called prior to startDocument().
    at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:210)
    at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:118)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:678)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:727)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
    at ScalesTransformTest$.main(ScalesTransformTest.scala:36)
    at ScalesTransformTest.main(ScalesTransformTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at sbt.Run.invokeMain(Run.scala:72)
    at sbt.Run.run0(Run.scala:65)
    at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
    at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
    at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
Caused by: org.xml.sax.SAXException: setResult() must be called prior to startDocument().
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.startDocument(TransformerHandlerImpl.java:183)
    at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.startDocumentInternal(ToSAXHandler.java:99)
    at com.sun.org.apache.xml.internal.serializer.SerializerBase.startDocument(SerializerBase.java:1235)
    at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartDocument(StAXStream2SAX.java:239)
    at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:153)
    at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:118)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:678)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:727)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
    at ScalesTransformTest$.main(ScalesTransformTest.scala:36)
    at ScalesTransformTest.main(ScalesTransformTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at sbt.Run.invokeMain(Run.scala:72)
    at sbt.Run.run0(Run.scala:65)
    at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
    at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
    at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
[debug]     Thread run-main exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
java.lang.RuntimeException: Nonzero exit code: 1
    at scala.sys.package$.error(package.scala:27)
    at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
    at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
    at scala.Option.foreach(Option.scala:236)
    at sbt.BuildCommon$class.toError(Defaults.scala:1628)
    at sbt.Defaults$.toError(Defaults.scala:34)
    at sbt.Defaults$$anonfun$runMainTask$1$$anonfun$apply$34$$anonfun$apply$35.apply(Defaults.scala:637)
    at sbt.Defaults$$anonfun$runMainTask$1$$anonfun$apply$34$$anonfun$apply$35.apply(Defaults.scala:633)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
    at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
    at sbt.std.Transform$$anon$4.work(System.scala:64)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:244)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
[error] (compile:runMain) Nonzero exit code: 1


推荐答案

问题是您需要在做dom时提供样式表和sax转换:

The problem is that you need to provide a stylesheet when doing dom and sax transformations:

// scales XML parsing
import scales.xml._
import ScalesXml._
//import scales.xml.trax._ // needed for the latest scales version

object ScalesTransformTest {

  def run() {
    val elem = Elem("trax"l)
    val doc = Doc(elem / elem)
    println( s"scales doc:\n${asString(doc)}" )

    import javax.xml.transform._
    import org.w3c.dom.Document

    val matchall = 
      """<?xml version="1.0"?>
         <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output encoding="UTF-8" />
       <xsl:template match="*">
         <xsl:copy-of select="." />
       </xsl:template>
     </xsl:stylesheet>"""

val tf = TransformerFactory.newInstance
val trax = tf.newTransformer(new stream.StreamSource(new java.io.StringReader(matchall)))

val wr = new java.io.StringWriter
val str = new stream.StreamResult(wr)
trax.transform(doc, str)
println( s"\nsource only:\n${wr.toString}" )

val sr = ScalesResult()
trax.transform(doc, sr)
println( s"\nroundtrip:\n${asString(sr.doc)}" )

val dr = new dom.DOMResult 
trax.transform(doc, dr)
val ddoc = dr.getNode.asInstanceOf[Document]
println( s"\nscales=>DOM:\n$ddoc" )

val sr2 = ScalesResult()
trax.transform(new dom.DOMSource(ddoc), sr2)
println( s"\nback from dom:\n${asString(sr2.doc)}" )

val stf = tf.asInstanceOf[sax.SAXTransformerFactory]
val th = stf.newTransformerHandler(new stream.StreamSource(new java.io.StringReader(matchall)))

val wr2 = new java.io.StringWriter
val str2 = new stream.StreamResult(wr2)
th.setResult(str2)

val strax = th.getTransformer

val xr = new sax.SAXResult(th)
strax.transform(doc, xr)
println( s"\nscales=>SAX:\n${wr2.toString}" )

  }

}

这篇关于如何将Scala Scales的XML转换为DOM / SAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 00:48