简体   繁体   English

怎么做“ builder.parse(存储在这个罐子里的xml文件)”?

[英]How to do “builder.parse(xml file stored inside of this jar)”?

I am trying to use a file object, with the path of a XML file inside of the current jar file, which is running, in builder.parse(not the absolute path to the xml file); 我试图在builder.parse中使用一个文件对象,该文件对象具有正在运行的当前jar文件内部的XML文件的路径(不是xml文件的绝对路径);

DocumentBuilder builder = dbf.newDocumentBuilder(); DocumentBuilder builder = dbf.newDocumentBuilder();

Document doc = builder.parse("resources/userConfig.xml"); 文档doc = builder.parse(“ resources / userConfig.xml”);

The code works in eclipse but doesn't in a exported jar file. 该代码可在eclipse中使用,但不能在导出的jar文件中使用。 When i run the exported jar it can't find the XML in C:\\Users... 当我运行导出的jar时,它在C:\\ Users中找不到XML。

For your case you need the one but last method below, that takes an InputStream. 对于您的情况,您需要以下一个但最后一个方法,该方法采用InputStream。 The last method is added as an example for your case with the file in a jar in the classpath. 最后一种方法是作为您的案例的示例添加的,文件放在类路径中的jar中。 You may want to do the exceptionhandling differently. 您可能需要以不同的方式进行异常处理。

public class XMLLib {

    public static DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    public static Document readXML(File file) {
        try {
            final DocumentBuilder builder = builderFactory.newDocumentBuilder();
            return builder.parse(file);
        } catch (ParserConfigurationException ex) {
           return null;
        } catch (SAXException | IOException ex) {
           return null;
        }
    }

    public static Document readXML(InputStream is) {
        try {
            final DocumentBuilder builder = builderFactory.newDocumentBuilder();
            return builder.parse(is);
        } catch (ParserConfigurationException ex) {
             return null;
        } catch (SAXException | IOException ex) {
            return null;
        }
    }

    //example call:
    public static Document getDocumentResource(String resourcepath){
        try (InputStream is = XMLLib.class.getResourceAsStream(resourcepath)){
             return readXML(is); 
         } catch (IOException ex) {}
         return null;
    }
}

Some more explaination: In the Eclipse environment the xml-file is extracted, so you can access it directly as a File(as it is a "File"). 更多说明:在Eclipse环境中,提取了xml文件,因此您可以将其作为文件直接访问(因为它是“文件”)。 Now with everything in a jar the xml doesn't exist as a "File" anymore so you have to extract it from the jar file. 现在,所有内容都放在jar中,xml不再作为“文件”存在,因此您必须从jar文件中提取它。 The classloader is able to do so via getResourceAsStream - just like it can read other resources and classes. 类加载器能够通过getResourceAsStream做到这一点-就像它可以读取其他资源和类一样。 So in essence, what this is doing is loading the xml like java loads a class. 因此,从本质上讲,这就是像java加载类那样加载xml。 For that to work, the resourcepath must be given with respect to the classpath. 为此,必须相对于类路径给出资源路径。 Only the InputStream version above will work(or some other aproach eg using Path and Filesystem) 仅上述InputStream版本适用(或其他一些方法,例如使用Path和Filesystem)

Usualy if you have a resource in "resources/userConfig.xml" the path is simple "/userConfig.xml". 通常,如果您在“ resources / userConfig.xml”中有一个资源,则路径是简单的“ /userConfig.xml”。 (but that depends on how the project is assembled) (但这取决于项目的组装方式)

I can only guess(depends on how the project is assembled) in your case you need: 我只能根据您的情况猜测(取决于项目的组装方式):

Document document = XMLLib.getDocumentResource("/userConfig.xml");

Important: the InputStream Version works with the file in the jar and with the file extracted(this is allways ) - as long as it can be found by the classloader. 重要提示:为InputStream版在罐子里用提取的文件的文件(这是百达 ) -只要它可以被加载器找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM