0
我試圖做一個簡單的DOM作者與讀者在內部存儲解析XML DOM文件
這是代碼
protected void write_xml_file(String file_name) {
//if (file_name == null) file_name = "spells.xml";
FileOutputStream fos;
try {
fos = openFileOutput(file_name, Context.MODE_APPEND);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "spells");
for (int j = 0; j < 3; j++) {
serializer.startTag(null, "spell");
serializer.text("a" + j);
serializer.endTag(null, "spell");
}
serializer.endDocument();
serializer.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void read_xml_file(String file_name) {
try {
File fXmlFile = new File(file_name);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("spells");
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("spell id : " + eElement.getAttribute("id"));
//System.out.println("name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
//System.out.println("description : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
//System.out.println("school : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
爲寫沒問題,該文件被正確地寫道:
當我嘗試閱讀我有一個錯誤:未找到文件
有人可以幫助我嗎?
感謝
莫羅