如何在android中解析这个字符串?

时间:2011-05-05 12:16:01

标签: android xml parsing

这是我的代码

try
        {
            url = new URL("http://localhost/abc//webservice.php?op=login");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");
            connection.setRequestProperty("user_email", "abc@abc.com");
            connection.setRequestProperty("user_password", "123456");


            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();

            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
            {
                String line = "";

                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                response = sb.toString();
                isr.close();
                reader.close();

这是我的回答:

<?xml version ='1.0' encoding ='UTF-8' ?>
   <root>
    <userid>36</userid>
    <message>Successfully Loggin in</message>
   </root>

请告诉我如何获得代码<userid><message>

的价值

1 个答案:

答案 0 :(得分:5)

您的回复是一个简单的xml。使用您想要的任何技术解析它:

  1. DOM and SAX
  2. JAXB
  3. SimpleXml