我在尝试从XML文件中读取时收到java.io.FileNotFoundException /employee.xml: open failed ENOENT (No such file or directory)
。
创建XML文件的类:
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("http://www.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();
}
}
}
从我的XML文件中读取并在其中添加的类:
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);
}
}
答案 0 :(得分:1)
我赞扬你的英语尝试。 Mais,je parle le Francais aussi。
Vous devez utiliser
File f = new File("employee.xml");
f.createNewFile();
avant vous utilisez vos程序。 Vous n'avez pas un fichier pour ecriver。 Donc,vous devez le creer。