Reputation: 67
So, I'm trying to open a XML document with java using docBuilder.parse(filepath)
This is what my code looks like:
public void openXMLfile(String filepath) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
}
catch(Exception e) {
e.printStackTrace();
}
}
I have written the absolute path of my file which is:
C:\\Users\\"my User account"\\Desktop
, being "my user account", replaced by the actual name of my user account
In my main function, which looks like this:
public static void main(String[] args) {
App aplication = new App();
String filepath = "C:\\Users\\"my User account"\\Desktop";
aplicacao.openXMLfile(filepath);
}
However, I get the following line of erros:
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)
Could anyone clarify me in what I might be doing wrong? May it be the filepath itself?
Thank you very much in advance!
Upvotes: 1
Views: 72
Reputation: 1804
The parse method takes a URI for the file location, so something along the lines of file:///c:/user/
This question gives examples - How should a file: URI corresponding to a Windows path name look like? particularly Paulo's comment.
Examples also at https://en.m.wikipedia.org/wiki/File_URI_scheme
Upvotes: 2