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.

michalmichalek
  • 250
  • 4
  • 19
  • "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

2 Answers2

0

Data to be read

<?xml version="1.0"?>
<company>
    <staff id="1001">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

Try out this java code its working very well

try {

    File fXmlFile = new File("/Users/mkyong/staff.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("staff");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("Staff id : " + eElement.getAttribute("id"));
            System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

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

Root element :company

Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000

Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000
Rhn Bhadani
  • 2,208
  • 1
  • 17
  • 25
  • Man I used that. You even check my code? Its copy of that. The problem is that is not working. Maybe there is something wrong with my path direction. I know the file is in this path but maybe I should write it in the other way? – michalmichalek Apr 06 '17 at 13:29
  • @MadMike6661 Can you show me your XML response...?? – Rhn Bhadani Apr 06 '17 at 13:33
  • @MadMike6661 Just convert your file-->>Document-->nodeList-->>fetch attribute – Rhn Bhadani Apr 06 '17 at 13:35
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
  • 651
  • 6
  • 14