Not able to append a new node into the mxl file

40 Views Asked by At

I'm trying to append a new node to an XMl file, there is neither error nor exception, still it is not being working as excepted. Can anybody suggest what might be the issue. Thanks in advance.

Code:

private void parseFile() throws ParserConfigurationException, Exception{
    FileInputStream fis = null;
    org.w3c.dom.Document dom = null;
    //File file = new File("D://TestWorkspace//test samples//src//com//test//pro//employees.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    /*fis = new FileInputStream(file);
    System.out.println("Total file size to read (in bytes) : "+ fis.available());*/
    DocumentBuilder db = dbf.newDocumentBuilder();

    //parse using builder to get DOM representation of the XML file
     dom = db.parse("D://TestWorkspace//testSamples//src//com//test//pro//employees.xml");

     Node allowedFiles = dom.getElementsByTagName("employee").item(0);
     Element newNode = dom.createElement("address");
     newNode.appendChild(dom.createTextNode("Bangalore"));
     allowedFiles.appendChild(newNode); 
}

XML file:

<?xml version="1.0" encoding="UTF-8"?> <employees>      <employee id="111">
  <firstName>Rakesh</firstName>
  <lastName>Mishra</lastName>
  <location>Bangalore</location>    </employee> </employees>

Regards Bamadeva

0

There are 0 best solutions below