2012-12-22 80 views
1

helo,我想讀取這個文件中的值:http://www.nbp.pl/kursy/xml/lastC.xml並用它們填充網格。我的應用程序下載此文件並將其保存在SDCard上,等待它運行。我的問題是我的代碼讀取空值。這就是:android,從SD卡中讀取xml文件中的值

@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    grid= (GridView)findViewById(R.id.grid); 

    try{  
      url = new URL("http://www.nbp.pl/kursy/xml/LastC.xml"); 

      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 

      connection.setRequestMethod("GET"); 


      connection.setDoOutput(true); 
      connection.connect(); 



      File dir = new File (android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/Kursy Walut"); 
       if(dir.exists()==false) 
        dir.mkdirs(); 

      File file = new File(dir, "kursywalut.xml"); 

     FileOutputStream fileoutput = new FileOutputStream(file); 

     InputStream inputstream = connection.getInputStream(); 

      int tempSize=0; 


      byte[] buffer = new byte[1024]; 

      while((tempSize = inputstream.read(buffer))>0) 
      { 

       fileoutput.write(buffer, 0, tempSize); 

      } 

fileoutput.close(); 

在我的SD卡存儲文件,並舉杯與信息

Toast.makeText(MainActivity.this, "pobrano do Kursy Walut/kursywalut.xml!!", Toast.LENGTH_SHORT).show(); 

    //here i have a array of string witch i will parse into my grid 

    String[] items=new String[13]; 

    InputStream in = new FileInputStream(file.getPath()); 

    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 

    Document doc = builder.parse(in); 
    NodeList words = doc.getElementsByTagName("pozycja"); 

    for(int i=0; i<words.getLength(); i++) 
    { 
     items[i]= ((Element)words.item(i)).getAttribute("kod_waluty"); 

    } 

    //i setText on some textView to check that i read xmlfile correctly. 

    text.setText("value: "+items[5]); 
     in.close(); 



     grid.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items)); 
} 
catch(Throwable t){Toast.makeText(this, t.toString(), Toast.LENGTH_LONG).show();} 


} 

這個代碼我TextView的文本僅是「值」後沒有任何價值從XML 和我的網格空。我假設我在閱讀XML文件時犯了錯誤。 如果有人可以看看這個,並幫助我解決它,我會非常高興。

+0

更新與更新代碼的問題。 –

回答

2

請使用下面的代碼從SD卡使用Dom Parser解析XML文件,它將解決您的問題。

MainActivity.java:-

public class MainActivity extends Activity { 

    ArrayList<String> mImageLink; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     try { 
      mImageLink = new ArrayList<String>(); 

      File file = new File("mnt/sdcard/kursywalut.xml"); 
      InputStream is = new FileInputStream(file.getPath()); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(new InputSource(is)); 
      doc.getDocumentElement().normalize(); 

      NodeList nodeList = doc.getElementsByTagName("image"); 

      for (int i = 0; i < nodeList.getLength(); i++) { 

       Node node = nodeList.item(i); 

       Element fstElmnt = (Element) node; 

       mImageLink.add(fstElmnt.getAttribute("link")); 

      } 
     } catch (Exception e) { 
      System.out.println("XML Pasing Excpetion = " + e); 
     } 
    } 
} 
+0

它沒有幫助; /我仍然在我的「kod_waluty」屬性中獲得空值。我發佈了鏈接到我的xml文件。 http://www.nbp.pl/kursy/xml/lastC.xml –