0

我正在使用一个类来读取网页的内容(它包含像 xml 这样的文本)并将其保存在字符串变量中。它工作正常。我现在需要做的是在这个字符串中找到一些标签的值。所以这是阅读页面的类。如您所见,我需要读取标签的方法“onPostExecute”。

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 解析器吗?我需要一些易于使用的东西,因为我只需要阅读一两个标签。这是包含标签的字符串示例:

<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>

谢谢

4

2 回答 2

1

您可以使用 android 默认 xml 解析器解析传入的 xml。更好地使用 Simple XML parser 看看这里

于 2013-10-01T11:05:10.840 回答
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

所以你可以尝试这两种选择,

希望对你有帮助

于 2013-10-01T11:06:05.653 回答