我试图将新节点附加到XMl文件,既没有错误也没有异常,但它仍然没有作为例外工作。任何人都可以提出可能存在的问题。提前谢谢。
代码:
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文件:
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee id="111"> <firstName>Rakesh</firstName> <lastName>Mishra</lastName> <location>Bangalore</location> </employee> </employees>
此致 巴马德瓦