I have a problem to using a DOMParserXML to take data stored in a xml file called: "infofermata.xml" and placed this path: "xml/infofermata.xml".
The first problem that I found is: "2246-2246/com.example.giacomob.myapplication W/System.err﹕ java.io.FileNotFoundException: /xml\infofermata.xml: open failed: ENOENT (No such file or directory)"
The class that will read in this XML file is:
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();
}
}
}
I supposed that the error is in: " String filePath = "xml\infofermata.xml";", ma that path aren't wrong...please, help me!!
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<fermata>
<idfermata>1</idfermata>
<nazione>Italia</nazione>
<paese>Lecce</paese>
<via>Viale Grassi</via>
</fermata>
Thanks :)