简体   繁体   中英

xml parsing dom file not found

i'm trying to make a simple dom writer and reader in internal storage

this is the code

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();
    }
}

for to write no problem, the file is wrote correctly

when i try to read i have an error: file not found

can someone help me?

thanks

Mauro

While storing your are storing in the internal file directory, but while retrieving you are just giving just the name

Example, its writing in data/data/[package name]/files/[file_name] but you are trying to retrieve from [file_name].

So instead of getting directly from file name while read xml file, you can try like this,

File fXmlFile = new File(context.getFilesDir() + File.separator + file_name);

Note: you can get getFilesDir() from Context, so try to send context in parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM