2013-10-01 65 views
0

我正在使用類來讀取網頁(它包含像xml這樣的文本)的內容並將其保存在字符串變量中。它工作正常。我現在需要做的是找到這個字符串中一些標籤的值。 所以這是讀取頁面的類。正如你所看到的那樣,我需要讀取標籤的方法是「onPostExecute」。如何解析包含xml標記(需要讀取兩個標記)的簡單字符串

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 

     for (String url : urls) { 
      DefaultHttpClient client = new DefaultHttpClient(); //create a new http client 
      HttpGet httpGet = new HttpGet(url); //create a new http request passing a valid url 
      try { 
       HttpResponse execute = client.execute(httpGet); //try to execute the http get request 
       InputStream content = execute.getEntity().getContent(); //prepare the input stream to read the bytes of the request 
       BufferedReader buffer = new BufferedReader(
         new InputStreamReader(content)); 
       String s = ""; 
       while ((s = buffer.readLine()) != null) { 
        response += s; //until is present a line to read, the response variable store the value of the lines 
       } 

      } catch (Exception e) { 
       Log.i("MyApp", "Download Exception : " + e.toString()); //Print the error if something goes wrong 
      } 
     } 
     Log.i("RESPONSE",""+response); 
     return response; //return the response 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     result = webPage.doInBackground(initConfiguration); //take the result from the DownloadWebPageTask class 
     Log.i("RESULT",""+result); 
     //find the price and format value from the result    
    } 

} 

那麼現在我該怎麼辦?你有一些建議嗎?我有使用某種xml解析器嗎?我需要一些易於使用的東西,因爲我只需要閱讀一個或兩個標籤。 這是包含標籤字符串的例子:你可以使用Android默認XML解析器解析傳入的XML

<products currency="EUR"> 
<product id="1" width="1796" height="1228" name="10 X 15 cm"> 
    <prices> 
    <price fixfee="0.5" from="50" price="0.65" /> 
    <price fixfee="0.10" from="20" price="0.70" /> 
    <price fixfee="0.10" from="0" price="0.75" /> 
    </prices> 
</product> 
<product id="2" width="3626" height="2422" name="20 X 30 cm"> 
    <prices> 
    <price fixfee="0.5" from="50" price="0.75" /> 
    <price fixfee="0.10" from="20" price="0.80" /> 
    <price fixfee="0.10" from="0" price="0.100" /> 
    </prices> 
</product> 

感謝

回答

1

我建議你使用Jsoup庫。這裏是很好的例子,可以爲您服務:今天

String html = "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\">" + 
      "  <book>" + 
      "   <string> book name </string>" + 
      "   <array>" + 
      "    <string> Name1 </string>" + 
      "    <string> Name2 </string>" + 
      "   </array>" + 
      "  </book></xml>"; 

    Document doc = Jsoup.parse(html, "", Parser.xmlParser()); 

    Element book = doc.select("book").first(); 

    Element bookName = book.getElementsByTag("string").first(); 

    Elements array = book.getElementsByTag("array"); 

    for (Element e : array.select("string")) { 
     //.... 
    } 

但是Android有他的一個解析器XmlPullParser

所以,你可以嘗試這兩個選項,

希望它會幫助你

+0

感謝您的建議,但如果可能,我更喜歡使用官方給出的東西從android sdk – Hieicker

+0

看到我的第二個選項,'XmlPullParser' –

+0

這是完美的感謝!我會看到更好的XmlPullParser! – Hieicker

1

。最好使用簡單的XML解析器看看here

+0

是的,我想用從SDK官方給出的解析器。你的意思是在這種情況下的XmlPullParser?或者是其他東西? – Hieicker

+0

其他的東西給你基於JABX類型註解的XML解析。看看這個教程。我個人使用它。 – Yup