强制Java在Web浏览器中打开XML [英] Force Java to open XML in web browser

查看:112
本文介绍了强制Java在Web浏览器中打开XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序(GUI = JavaFX),该程序会在文件系统中扫描特定的XML报告文件,并将它们列出在表格中。这些XML报告由XSLT在Web浏览器中呈现。现在,我希望能够在Java应用程序中单击此类报告,并将其显示在浏览器中。我已经编写了处理程序,并确定了正确的URL。在我的Windows系统上,这是

I have a program (GUI = JavaFX), which scans my file system for specific XML report files and lists them in a table. These XML reports are rendered in a web browser by XSLT. Now I want to be able to click on such a report in my Java application and have it displayed in the browser. I already wrote the handler and the correct URL is determined. On my Windows system this is

file://localhost/C:/report.xml

XML不是问题。如果我在浏览器中手动打开它,则一切正常。但是,如果我使用Google并询问如何在浏览器中打开文件,它将始终为我提供以下信息:

The XML is not the problem. If I open it manually in my browser everything works fine. However if I use Google and ask how to open files in a browser it always gives me this:

java.awt.Desktop.getDesktop().
    browse(new java.net.URI("file://localhost/C:/report.xml"));

由于这是http URL(网站)的一个很好的解决方案,因此它总是在我的默认文本编辑器(例如Notepad ++)。因此,桌面的浏览方法并没有真正强制浏览,而只是退回到默认的打开操作。

As this is a good solution for http URLs (web sites), it always opens my XML file in my default text editor (e.g. Notepad++). So the browse method of the Desktop doesn't really force the browse, but merely falls back to a default open operation.

所以问题是:我该如何强制Java在浏览器中打开XML类似于Windows函数用...打开?

So the question is: How can I force Java to open the XML in the browser similar to the Windows function "Open with >"?

这里是sscce(即使在浏览器中也应尝试访问文件,即使它不存在):

Here an sscce (which should try to access the file in a browser, even though it doesn't exist):

public class XMLOpener {
  public static void main(String[] args) 
  {
    String fileURL = "file://localhost/C:/report.xml";
    try {
      java.awt.Desktop.getDesktop().browse(new java.net.URI(fileURL));
    } catch (Exception e) {}
  }
}


推荐答案

awt Desktop.browse方法的JavaFX替换为 HostServices.showDocument 。您可以尝试这样做,但是它可能具有与Desktop.browse相同的效果。

The JavaFX replacement for the awt Desktop.browse method would be HostServices.showDocument. You could try that, but it will likely have the same the same effect as Desktop.browse.

另一种替代方法是在Java中加载XML并执行XSL转换,然后使用 webview.getEngine.loadContent (contentString,contentType),或仅在Label或自定义JavaFX控件中显示结果文档。请注意,从JavaFX 2.2开始,JavaFX WebView尚没有用于漂亮打印xml内容类型的查看器,因此要在webview中获得漂亮打印的xml,您可能需要使用javascript /将xml解析并格式化为html文档。 css / html,类似于此文章中在WebView中显示格式化的Java源代码中演示的方法。

Another alternative is to load the XML and perform the XSL transform in Java, then display the resulting document in a JavaFX WebView using webview.getEngine.loadContent(contentString, contentType) or just display the resultant document in a Label or custom JavaFX control. Note that, as of JavaFX 2.2, the JavaFX WebView does not yet have a viewer for pretty printing an xml content type, so to get pretty printed xml in the webview you may need to parse and format the xml as an html document using javascript/css/html, similar to the method demonstrated in this post for displaying formatted java source in a WebView.

对我来说,尽管这是更多的开发工作,但是这种用JavaFX处理显示的替代方法很好,因为可以将结果文档的显示封装在JavaFX中并进行控制。应用程序本身,而您不必依赖于宿主环境中可能安装或可能未安装的任何浏览器和配置。

For me, although it's more development work, this alternate approach of handling the display with JavaFX is nice because the display of the resultant document can be encapsulated and controlled in the JavaFX application itself and you don't have a reliance on whatever browser and configuration may or may not be installed in the host environment.

这篇关于强制Java在Web浏览器中打开XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆