0

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 :)

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49

1 Answers1

0

Giacomo, the res folder is reserved for application resources, then to read a file in it you have to use the getResources() method, and then the getXml(). This example is a little bit outdated, but I believe can help you.

If you want to use the DocumentBuilderFactory and the other java class for DOM, I suggest you to put your xml in the 'assets' folder of your app, the you get the InputStream using:

AssetManager assetManager = getAssets();
InputStream is = assetManager.open("infofermata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
[...]
is.close();

Hope it can help.

Mimmo Grottoli
  • 5,758
  • 2
  • 17
  • 27
  • Thanks @mimmo grottoli.. but you're italian? Sei italiano? così parliamo nella nostra lingua madre magari, così forse capisco un po' meglio.. –  Jul 30 '15 at 13:12
  • I'd like but I think it's not possibile :) – Mimmo Grottoli Jul 30 '15 at 13:15
  • okok..:) This is my first experience in Android. From your answer I understand that I have to create a new directory (in main) called "assets" and I put in this directory the xml file, true? –  Jul 30 '15 at 13:19
  • Yes, AndroidStudio should automaticcaly apply a custom icon to that folder (the same of the res folder) – Mimmo Grottoli Jul 30 '15 at 13:20
  • nothing... the same error... The sintax it's true? String filePath = "assets/infofermata.xml"; File fXmlFile = new File(filePath); –  Jul 30 '15 at 13:26
  • No! You don't need to open a file. Look at the code I wrote: `AssetManager assetManager = getAssets(); ...` That is the way to get a file from assets folder. – Mimmo Grottoli Jul 30 '15 at 13:28
  • ahhhhh.. ok, but now the method "getAssets();" is red: "Cannot resolve method 'getAssets()'" –  Jul 30 '15 at 13:34
  • Ok - of course you need a Context. If you're launching the method to read the file from an Activity, pass the reference to that Activity. `public static void readXmlFile(Context context) { AssetManager assetManager = context.getAssets(); ...}` and from Activity `ReadXMLFile.readXmlFile(this);` – Mimmo Grottoli Jul 30 '15 at 13:38
  • ok, in Main activity i have to delete: ReadXMLFile a = new ReadXMLFile();? and i have to add ReadXMLFile.readXmlFile(this);? –  Jul 30 '15 at 13:44
  • This is what I would do, make the readXmlFile a static method. – Mimmo Grottoli Jul 30 '15 at 13:45
  • ok, I have substituted ReadXMLFile a = new ReadXMLFile(); in ReadXMLFile.ReadXMLFile(this); the class ReadXMLFIle will appear: public class ReadXMLFile { public static void readXMLFile(Context context) { try { Log.i("MyActivity", "casa"); AssetManager assetManager = getAssets(); InputStream is = assetManager.open("infofermata.xml"); DocumentBuilderFactory dbFactory = ecc ecc.. but geaAssest is red yet...cannot resolve metod getAssests... –  Jul 30 '15 at 13:51
  • It Work!! Thanks thanks!! You're the best!!!! :) there is a way to vote your answer ? –  Jul 30 '15 at 13:58