2012-05-11 68 views
8

Próbuję czytać ten sam plik „xmlfile.xml” z aktywów folder i również inny egzemplarz z karty SD sdcard/download/.odczytać pliku XML z katalogu Android Aktywów

mogę czytać z karty SD:

  • unfile Return Prawdziwego
  • Ofertę Return prawda

ja nie nie można odczytać z katalogu aktywa:

  • unfile Return Fałsz
  • Ofertę Powrót Fałsz

Ten kod nie działa il

 File source = new File("file:///android_asset/xmlfile.xml"); 
     boolean unfile = source.isFile(); 
     boolean Esiste = source.exists(); 

     try 
     { 
      // todo 
     } catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

Ten kod il Praca

 File source = new File("/sdcard/" + "download" + "/" + "xmlfile.xml"); 
     boolean unfile = source.isFile(); 
     boolean Esiste = source.exists(); 

     try 
     { 
      // todo 
     } catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

ktoś może mi wyjaśnić, jak mogę odczytać plik z folderu Zasoby.

dzięki marco

+0

Zobacz an ans podane przeze mnie na http://stackoverflow.com/questions/1372470/parse-local-xml-file-in-android/8413361#8413361 – Khan

Odpowiedz

11

Aby otworzyć zasób należałoby następujący fragment kodu:

InputStream is = getAssets().open("xmlfile.xml") 
6

użyć tej funkcji

// Converting given XML file name to String form 
String mOutputText = getxml("yourxml.xml"); 

/** 
* Function used to fetch an XML file from assets folder 
* @param fileName - XML file name to convert it to String 
* @return - return XML in String form 
*/ 
private String getXml(String fileName) { 
    String xmlString = null; 
    AssetManager am = context.getAssets(); 
    try { 
     InputStream is = am.open(fileName); 
     int length = is.available(); 
     byte[] data = new byte[length]; 
     is.read(data); 
     xmlString = new String(data); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
    return xmlString; 
} 
+0

Dla pliku mam na myśli Class [link] (http://developer.android.com/reference/java/io/File.html) – marcoqf73

+0

zamień InputStream is = am.open (ścieżka); z InputStream is = new FileInputStream (File) Myślę, że to może pomóc – Mufazzal

0
try 
    { 
     AssetManager aManager = Class.getAssets(); 
     InputStream iStream = aManager.open("file.xml"); 
     int length = iStream.available(); 
     byte[] data = new byte[length]; 
     iStream.read(data); 
     assetString = new String(data).toString(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
0

Będzie to z pewnością pomogą Ci . Do odczytywania folderu z plikami formularzy potrzebny jest obiekt InputStream.

Składnia:

InputStream yourobj=getApplicationContext().getAssets().open("Path to xml file"); 

Więc kod na żywo może być:

InputStream in_s = getApplicationContext().getAssets().open("www/application/app/client/controllers/data1.xml"); 

data1.xml Tutaj jest plik wewnątrz ścieżki.

Powiązane problemy