Force Java to open XML in web browser

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 05:44:07

问题


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

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"));

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.

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

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) {}
  }
}

回答1:


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.

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.

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.




回答2:


If the browser can't handle the URI, browse launches "the application registered for handling URIs of the specified type is invoked".

Check your favourite browser exists (Chrome, Fx, O, IE, etc) or get it by some other method, and then execute a custom command. If you know the OS you're running on (windows) then you only need to consider the exec line for that.



来源:https://stackoverflow.com/questions/12073356/force-java-to-open-xml-in-web-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!