我有一个问题,就是使用DOMParserXML来获取存储在名为“infofermata.xml”的xml文件中的数据并放置此路径:“xml / infofermata.xml”。
我发现的第一个问题是:"2246-2246/com.example.giacomob.myapplication W/System.err﹕ java.io.FileNotFoundException: /xml\infofermata.xml: open failed: ENOENT (No such file or directory)"
将在此XML文件中读取的类是:
package com.example.giacomob.myapplication;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class ReadXMLFile {
public ReadXMLFile() {
try {
Log.i("MyActivity", "casa");
String filePath = "xml\\infofermata.xml";
File fXmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("fermata");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
// Log.i("MyActivity", "casa");
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// System.out.println("Staff id : " + eElement.getAttribute("id"));
// String stringidfermata = "Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent()"";
// Log.i("MyActivity", "\"Id Fermata : \" + eElement.getElementsByTagName(\"idfermata\").item(0).getTextContent()");
System.out.println("Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent());
System.out.println("Naziome : " + eElement.getElementsByTagName("nazione").item(0).getTextContent());
System.out.println("Paese : " + eElement.getElementsByTagName("paese").item(0).getTextContent());
System.out.println("Via : " + eElement.getElementsByTagName("via").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我认为错误发生在:“String filePath =”xml \ infofermata.xml“;”,那道路没有错...请帮帮我!!
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<fermata>
<idfermata>1</idfermata>
<nazione>Italia</nazione>
<paese>Lecce</paese>
<via>Viale Grassi</via>
</fermata>
谢谢:)
答案 0 :(得分:0)
Giacomo,res文件夹是为应用程序资源保留的,然后要读取其中的文件,你必须使用getResources()方法,然后使用getXml()。这个example有点过时,但我相信可以帮到你。
如果你想使用DocumentBuilderFactory和DOM的其他java类,我建议你把你的xml放进去你的应用程序的'assets'文件夹,你得到InputStream使用:
AssetManager assetManager = getAssets();
InputStream is = assetManager.open("infofermata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
[...]
is.close();
希望它可以提供帮助。