从swing导入xml文件 [英] Import a xml file from package swing

查看:206
本文介绍了从swing导入xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从包中导入shutdownscheduler.xml文件,但出现错误:

I am using this code for import shutdownscheduler.xml file from my package but I am getting error:

public static Document handler() throws ParserConfigurationException, SAXException, IOException{
        String s=xmlhandler.class.getResource("shutdownscheduler.xml").toString();
        File newFile=new File(s);
        DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder documentbuilder=documentbuilderfactory.newDocumentBuilder();
        Document document=(Document)documentbuilder.parse(newFile);
        document.getDocumentElement();
        return document;
    }



Exception in thread "main" java.io.FileNotFoundException:
C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown
Scheduler\file:\C:\Users\Rohan%20Kandwal\Documents\NetBeansProjects\Shutdown%20Scheduler\build\classes\shutdown\scheduler\shutdownscheduler.xml
(The filename, directory name, or volume label syntax is incorrect)

如果我提供文件的直接路径,则导入工作正常.但是,一旦1创建项目的jar文件并在其他系统上运行它,将导致错误.因此,我只想从包中导入文件.

If I give a direct path of the file then the import is working correctly. But this will result in an error once 1 create a jar file of the project and run it on other system. So I want a way to import file from my package only.

文件的正确路径是C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler\shutdownscheduler.xml

推荐答案

尝试使用InputStream而不是File

documentbuilder.parse( xmlhandler.class.getResourceAsStream("shutdownscheduler.xml"));

这篇关于从swing导入xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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