【问题标题】:xml parsing dom file not found找不到xml解析dom文件
【发布时间】:2017-12-19 13:06:45
【问题描述】:

我正在尝试在内部存储中制作一个简单的 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();
    }
}

为了写入没问题,文件写入正确

当我尝试阅读时出现错误:找不到文件

有人可以帮我吗?

谢谢

毛罗

【问题讨论】:

    标签: java android xml dom


    【解决方案1】:

    在存储时您正在存储在内部文件目录中,但在检索时您只是给出名称

    例如,它的写法是 data/data/[package name]/files/[file_name] 但您正试图从 [file_name] 中检索。

    所以在读取xml文件时不要直接从文件名中获取,你可以这样尝试,

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

    注意:你可以从上下文中获取getFilesDir(),所以尝试发送上下文 在参数中。

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 2011-12-15
      • 2010-12-25
      • 2012-04-24
      • 2020-01-20
      • 2015-12-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      相关资源
      最近更新 更多