java.io.FileNotFoundException /employee.xml: open failed ENOENT (No such file or directory) on file read

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Mar 5, 2015 11:31:42 AM via Website

when trying to read from an XML file.

Class to create my XML file:

public class Filesauv {
public static void lance() {

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.newDocument();
        //add elements to Document
        Element rootElement =
            doc.createElementNS("journaldev.com/application", "Applications");
        //append root element to document
        doc.appendChild(rootElement);


        //for output to file, console
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        //for pretty print
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);

        //write to console or file
        StreamResult console = new StreamResult(System.out);
        StreamResult file = new StreamResult(new File("employee.xml"));

        //write data
        transformer.transform(source, console);
        transformer.transform(source, file);
        System.out.println("DONE");

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



Class to read from my XML file and add inside it:


public class ModifyXMLDOM extends MainActivity {
  public static void lance(){

      String filePath = "employee.xml";
        File xmlFile = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();


            //add new element
            addElement(doc);

            //write the updated document to file or console
            doc.getDocumentElement().normalize();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("employee.xml"));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source, result);
            System.out.println("XML file updated successfully");

        } catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
            e1.printStackTrace();
        }

}

private static void addElement(Document doc) {
    NodeList employees = doc.getElementsByTagName("Applications");
    Element emp = null;

    //loop for each employee

        emp = (Element) employees.item(0);
        Element appliElement = doc.createElement("application");
        Element serviceElement1 = doc.createElement("messagerie");
        Element serviceElement2 = doc.createElement("repertoire");
        Element serviceElement3 = doc.createElement("calculatrice");
        Element serviceElement4 = doc.createElement("calendrier");

        serviceElement1.appendChild(doc.createTextNode(m));
        serviceElement2.appendChild(doc.createTextNode(re));
        serviceElement3.appendChild(doc.createTextNode(ca));
        serviceElement4.appendChild(doc.createTextNode(cale));

        appliElement.appendChild(serviceElement1);
        appliElement.appendChild(serviceElement2);
        appliElement.appendChild(serviceElement3);
        appliElement.appendChild(serviceElement4);

        emp.appendChild(appliElement);
    }
}