如何解析包含xml标签的简单字符串(需要读取两个标签)

时间:2013-10-01 11:01:41

标签: android xml-parsing

我正在使用一个类来读取网页的内容(它包含像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>

由于

2 个答案:

答案 0 :(得分:1)

您可以使用android默认xml解析器解析传入的xml。更好地使用简单的XML解析器看看here

答案 1 :(得分: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

所以你可以试试这两个选项,

希望它能帮到你