3

I'm using JDOM with SAXBuilder to parse XML files, and I have a problem with a file which is throwing this error :

java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
    at com.foo.moo.MyClass.getValues(MyClass.java:321)

And the line is :

Document document = null; 
document = sxb.build(files.elementAt(i)); // This one

After doing some research on the internet, I think the error is related to Java 1.6.

What do you think ?

MMAdams
  • 1,508
  • 15
  • 29
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

3 Answers3

10

My first guess is that you are loading a local file, like: "c:/mylocalfile.xml"

But this URL does not contain the protocol, like http://, ftp:// or file://

Try file://c:/mylocalfile.xml

Kennet
  • 5,736
  • 2
  • 25
  • 24
  • 1
    Agree. You can try something like `if(myUrl.getProtocol().equals("c")){myUrl = new URL("file://"+myUrl.toexternalform())}` – MrDrews Aug 30 '11 at 13:44
4

Actually, I had space in my folder name which caused this error. Even in case of local files JAVA has the capability of understanding the protocol types.

Prabakaran
  • 41
  • 1
  • Yup, you are so right, and as the OP mentioned this seems to be in issue in Java 1.6 and above only. – zEro Jun 13 '13 at 11:12
0

I had a space in my file name

i solved it using an InputStream instead of the filename:

works:

fis = new FileInputStream(filename);
Document doc = new SAXBuilder().build(fis);

crashes:

Document doc = new SAXBuilder().build(filename);
AlexWien
  • 28,470
  • 6
  • 53
  • 83