简体   繁体   中英

Android:Unable to read from xml

While trying to read from xml , I am getting IOException .

Following is the code

File xmlFile = new File("\\assets\\data\\data.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);

dBuilder.parse(xmlFile) is throwing an IOException .

I have tried new File("/assets/data/data.xml") and new File("assets/data/data.xml") also but with no use. What could be the possible error and how can it be solved?

You do not access assets/ at runtime using File . You should access assets/ at runtime using AssetManager , which you can get via getResources().getAssets() .

For more info see Android parse XML file from Assets or internal/external storage

Get xml from Assets as

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(getXml(context,"data/data.xml")));
Document doc =  builder.parse(is);

And function is

private String getXml(Context mContext,String path){

    String xmlString = null;
    AssetManager am = mContext.getAssets();
    try {
        InputStream is = am.open(path);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        xmlString = new String(data);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return xmlString;
}

Please try Something like this

AssetManager assetManager = getBaseContext().getAssets();

 try{
   InputStream is = assetManager.open("data.xml");
   SAXParserFactory spf = SAXParserFactory.newInstance();
   SAXParser sp = spf.newSAXParser();
   XMLReader xr = sp.getXMLReader();

   OrderXMLHandler myXMLHandler = new OrderXMLHandler();
   xr.setContentHandler(myXMLHandler);
   InputSource inStream = new InputSource(is);
   xr.parse(inStream);

   String dataField1 = myXMLHandler.getDataField1(); //Use your fields in the data.xml file
   String dataField2 = myXMLHandler.getDataField2(); //Use your fields in the data.xml file
 is.close();

 } catch (Exception e) {
    e.printStackTrace(); 
 }

Just copy your file to the Internal Cache of your Application like this

  InputStream xmlIS = getAssets.open("data/data.xml");

  File xmlFile = new File(getCacheDir(), "data.xml");
  FileOutputStream xmlStream = new FileOutputStream(xmlFile);

  byte[] buffer = new byte[1024];
  int read = -1;
  while((read = xmlIs.read(buffer)) > 0) {
        xmlStream.write(buffer, 0, read);
        xmlStream.flush();
  }

Now you can set xmlFile as input of dBuilder.parse(xmlFile);

If you want to save it permanently change, getCacheDir() to getFilesDirectory() and do something like this

  File xmlFile = new File(getFilesDirectory(), "data.xml");
  if(!xmlFile.exists()) {
  ... // the code which is above
  }

then pass xmlFile to dbBuilder.parse function.

hope it will help.

regards Moonzai

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