使用Saxon-HE在JavaFX中对xml文件进行XSLT转换

时间:2014-10-28 22:39:23

标签: java xml xslt javafx saxon

我正在构建一个小应用程序,我打开一个XML文件并在JavaFX中的TextArea中显示它。然后我调用一个指向本地XSL文件的方法,该文件执行转换为简单的HTML输出。最后,我想将转换后的XML显示到一个新的弹出窗口中(加载另一个.fxml文件)。

所以我有一个RootLayout.fxml和一个相应的RootLayoutController来处理变换调用。 RootLayout很简单,只处理菜单操作(打开文件,转换)并启动另一个FXML。 TextArea包含在此其他FXML文件editorOverview中:

@FXML
private void handleTransform() {
    String xslt = "main.xsl"; // the path is reduced only here for simplicity
    String source = src.MainApp.editorOverviewController.XMLtextArea.getText();
    String result = "";
    TransformerFactory tFactory = TransformerFactory.newInstance();
    try {    
        Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xslt)));
        transformer.transform(new StreamSource(new StringBufferInputStream(source)), new StreamResult(result));

        src.MainApp.TransformViewController.resultTextArea.setText(result);
        src.MainApp.TransformViewController.showResult();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

所以我试图将转换结果分配给空字符串result,然后将TransformView.fxml中文本区域的文本设置为`result的值。在那之后,我想要显示这个窗口。

在我的TransformViewController我有showResult()方法:

public void showResult() {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("TransformView.fxml"));
        Parent htmlView = (Parent) loader.load();
        Stage stage = new Stage();
        stage.setTitle("HTML Result");
        stage.setScene(new Scene(htmlView));
        stage.show();
    } catch (IOException ex) {
        Logger.getLogger(TransformViewController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

问题是我收到以下错误:

java.io.FileNotFoundException: (Access is denied)
; SystemID: 
net.sf.saxon.trans.XPathException: java.io.FileNotFoundException: (Access is denied)

错误是否与我进行转换的方式有关? (为了记录,转换在带有Saxon-HE 9.5的oXygen XML Developer中成功执行,与我作为jar包含在我的项目中一样)

非常感谢任何想法/建议!

修改

堆栈跟踪:

Error on line 33 of main.xsl:
  java.io.FileNotFoundException: C:\NetBeansProjects\editor
  (Access is denied)
; SystemID: file:/C:/XSLT/main.xsl; Line#: 33; Column#: -1
net.sf.saxon.trans.XPathException: java.io.FileNotFoundException: C:\NetBeansProjects\editor (Access is denied)
    at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:198)    at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:198)
    at net.sf.saxon.serialize.Emitter.makeWriter(Emitter.java:160)
    at net.sf.saxon.serialize.XMLEmitter.openDocument(XMLEmitter.java:145)
    at net.sf.saxon.serialize.XMLEmitter.characters(XMLEmitter.java:570)
    at net.sf.saxon.serialize.XMLIndenter.indent(XMLIndenter.java:311)
    at net.sf.saxon.serialize.XMLIndenter.startElement(XMLIndenter.java:111)
    at net.sf.saxon.event.NamespaceReducer.startElement(NamespaceReducer.java:73)
    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:558)
    at net.sf.saxon.event.ComplexContentOutputter.startElement(ComplexContentOutputter.java:183)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:424)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
    at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:660)
    at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:138)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:431)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
    at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239)
    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1057)
    at net.sf.saxon.Controller.transformDocument(Controller.java:2088)
    at net.sf.saxon.Controller.transform(Controller.java:1911)
    at uimlbuddy.view.RootLayoutController.handleTransform(RootLayoutController.java:170)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8216)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
    at com.sun.glass.ui.View.notifyMouse(View.java:925)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/501263526.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: C:\NetBeansProjects\editor (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:192)
    ... 80 more
---------
java.io.FileNotFoundException: C:\NetBeansProjects\editor (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:192)
    at net.sf.saxon.serialize.Emitter.makeWriter(Emitter.java:160)
    at net.sf.saxon.serialize.XMLEmitter.openDocument(XMLEmitter.java:145)
    at net.sf.saxon.serialize.XMLEmitter.characters(XMLEmitter.java:570)
    at net.sf.saxon.serialize.XMLIndenter.indent(XMLIndenter.java:311)
    at net.sf.saxon.serialize.XMLIndenter.startElement(XMLIndenter.java:111)
    at net.sf.saxon.event.NamespaceReducer.startElement(NamespaceReducer.java:73)
    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:558)
    at net.sf.saxon.event.ComplexContentOutputter.startElement(ComplexContentOutputter.java:183)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:424)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
    at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:660)
    at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:138)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:431)
    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
    at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239)
    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1057)
    at net.sf.saxon.Controller.transformDocument(Controller.java:2088)
    at net.sf.saxon.Controller.transform(Controller.java:1911)
    at uimlbuddy.view.RootLayoutController.handleTransform(RootLayoutController.java:170)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8216)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
    at com.sun.glass.ui.View.notifyMouse(View.java:925)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/501263526.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

要以字符串形式获得转换结果,请执行

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);

指定result作为转化的目标(第二个参数为transform()),完成后,sw.toString()将包含序列化的转换结果。