如何用Java打开XML文档

问题描述 投票:0回答:1

所以,我正在尝试使用docBuilder.parse(filepath)用Java打开XML文档>

这是我的代码的样子:

    public void openXMLfile(String filepath) {
        try { 
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

我已写出文件的绝对路径为:

[C:\\Users\\"my User account"\\Desktop,是“我的用户帐户”,由我的用户帐户的实际名称代替

在我的主要功能中,看起来像这样:

    public static void main(String[] args) {

        App aplication = new App();

        String filepath = "C:\\Users\\"my User account"\\Desktop";
        aplicacao.openXMLfile(filepath);
    }

但是,出现以下错误:

java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at Trabalho.App.openXMLfile(App.java:39)
    at Trabalho.App.main(App.java:70)

有人能说明我做错了什么吗?可能是文件路径本身吗?

非常感谢!

因此,我正尝试使用docBuilder.parse(filepath)使用Java打开XML文档,这就是我的代码:public void openXMLfile(String filepath){试试{...

java xml file dom path
1个回答
0
投票
parse方法采用URI作为文件位置,所以类似于file:/// c:/ user /的内容>

此问题给出示例-How should a file: URI corresponding to a Windows path name look like?

© www.soinside.com 2019 - 2024. All rights reserved.