0

I have really annoying problem. I was looking for resolve on google and stackoverflow, trying to fix it but I tried a lot of solutions and it is still not working.

I want to read XML Data from XML File using DocumentBuilder.

This is my code:

public void LoadFromFile(){

    try{
        //File fXmlFile = new File("fishlist.xml");
        DocumentBuilderFactory dbFactory = 
        DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse("file:///C:/fishlist.xml"); // <--- there is exception run
        doc.getDocumentElement().normalize();


    }catch(Exception ex){
        ex.printStackTrace();
    }
}

and when I'm trying to run this method, it shows me Exception:

W/System.err: java.io.FileNotFoundException: /C:/fishlist.xml: open failed: 
ENOENT (No such file or directory)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err:     at             
libcore.net.url.FileURLConnection.connect(FileURLConnection.java:123)
W/System.err:     at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.jav
a:117)
W/System.err:     at 
javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:155)

W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err:     at libcore.io.Posix.open(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)

And yes. I have this file in that direction. Please help.

mmm
  • 260
  • 4
  • 21
  • "This exception... will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing." – Ishnark Apr 06 '17 at 13:23

1 Answers1

0

A potential workaround for me in the past has been creating a File object for the file.

File xmlFile = new File("C:/fishlist.xml");

And then tou can do one of two things:

  1. Create a FileInputStream for the File and pass that into your DocumentBuilder's parse method
  2. Continue to send the uri as you do by:

    Document doc = dBuilder.parse(xmlFile.toURI().toString);

Ishnark
  • 661
  • 6
  • 14
  • I was tried it that way... i don't know but something is not working. It should work but it isn't. I'll keep try. – mmm Apr 07 '17 at 10:19