如何使用android解析来自http服务器的xml

时间:2011-09-22 15:45:24

标签: android xml-parsing

  

可能重复:
  Parsing the XML Response in an Android Application

我正在使用以下代码

     public void executeHttpGet() throws Exception {
        dialog = new ProgressDialog(this);
        dialog.setCancelable(true);

        // set a message text
        dialog.setMessage("Loading...");

        // show it
        dialog.show();
         BufferedReader in = null;

         DefaultHttpClient client = new DefaultHttpClient();
         HttpGet request = new HttpGet();
         String url=   "http://newdev.objectified.com/morris/interface/mobile.php?method=dealerLogin&username=zzzzz&password=zzzz";

         String login = "zzzzz";
         String pass = "zzzzz";

         client.getCredentialsProvider().setCredentials(new AuthScope("newdev.objectified.com", 80), new UsernamePasswordCredentials(login, pass));

         request.setURI(new URI(url));
         HttpResponse response = client.execute(request);

                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String page = sb.toString();
                Toast.makeText(this, page, Toast.LENGTH_LONG).show();
             //  dialog.dismiss();


        }

现在我要解析即将推出的xml

<?xml version="1.0"?>
<response>
 <sid>kjdci120ts32e30akcj42acaf0</sid>
  <dnumber>dealer</dnumber>
 <dtitle>Test User</dtitle>
  <dcity>karachi</dcity>
  <dprovince>sindh</dprovince>

  <dcountry></dcountry>
  </response>

请帮忙

1 个答案:

答案 0 :(得分:2)

使用SAXDOMXmlPullParser,所有这些都可在Android中使用。