簡體   English   中英

從sdcard android解析XML

[英]Parse XML from sdcard android

我正在嘗試從SD卡讀取xml文件。 我需要將其解析為字符串數組。

XML(a.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string-array name="Timetable">
        <item>05:40</item>
        <item>06:00</item>
        <item>06:16</item>
        <item>06:28</item>
        <item>06:40</item>
        <item>07:16</item>
        <item>07:29</item> 
        <item>07:43</item>
        <item>07:55</item>
        <item>08:07</item>
        <item>08:22</item> 
        <item>08:34</item>
        <item>08:46</item>
        <item>08:58</item>
        <item>09:10</item>
        <item>09:22</item>
        <item>09:34</item>
        <item>09:46</item>        
        <item>12:10</item>
        <item>12:22</item>
        <item>12:34</item>
        <item>12:46</item>         
    </string-array>
</resources>

我嘗試使用兩個不同的函數,但沒有一個起作用,最后一個字符串為空。

功能一:

 public void function(){
        String xml = null;
        try {           
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("FindMyBus/a.xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

功能2:

public void prueba(){
        File file = new File(Environment.getExternalStorageDirectory()
                + "FindMyBus/a.xml");

        StringBuilder contents = new StringBuilder();
        try {
              //use buffering, reading one line at a time
              //FileReader always assumes default encoding is OK!
              BufferedReader input =  new BufferedReader(new FileReader(file));
              try {
                String line = null; //not declared within while loop
                /*
                * readLine is a bit quirky :
                * it returns the content of a line MINUS the newline.
                * it returns null only for the END of the stream.
                * it returns an empty String if two newlines appear in a row.
                */
                while (( line = input.readLine()) != null){
                  contents.append(line);
                  contents.append(System.getProperty("line.separator"));

                }
              }
              finally {
                input.close();
              }
            }
            catch (IOException ex){
              ex.printStackTrace();
            }

            String data= contents.toString();

    }

誰能幫我?

如果您需要解析xml文件,則需要使用xml解析器。 將其作為單獨的字符串獲取,然后將其保存到字符串數組中

請點擊此鏈接以更好地了解

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

當您獲得解析的字符串時,您可以將其添加到數組中。

如果您的xml位於應用程序資源中,就像看起來一樣,您可以執行以下操作:

String[] array = getResources().getStringArray(R.array.Timetable); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM