0

我是 android 编程新手。我的要求是调用 Web 服务。我成功地得到了来自 Web 服务的响应。如何在 android 中解析响应。给我解决方案。

这是获取响应的代码:

 HttpResponse response = httpclient.execute(httppost);
           String str=response.getStatusLine().toString();

           System.out.println("========URL STATUS========"+str);

           HttpEntity r_entity = response.getEntity();

           if( r_entity != null ) {
               result = new byte[(int) r_entity.getContentLength()];
               if(r_entity.isStreaming()) {
                    is = new DataInputStream(r_entity.getContent());
                   is.readFully(result);

               }
           }

           httpclient.getConnectionManager().shutdown();
           String responsedata= (new String(result).toString());
4

3 回答 3

3

以下示例用于 dom 解析器。

 DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
 DocumentBuilder db = dbf.newDocumentBuilder();
 InputSource is = new InputSource();
 StringReader sr=new StringReader(result.toString());
 is.setCharacterStream(sr);
 Document doc = db.parse(is);
 NodeList nodes = doc.getElementsByTagName("your root tag");

 //get your other tag elements.

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/。dom 解析器的示例。

http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/。萨克斯解析器的示例。

Dom 是基于 w3c 的解析器。Dom 比 sax 慢,因为它使用树节点并且必须在内存中。所以不推荐使用DOM解析器解析大数据。

另一方面,SAX 比 dom 快。推荐用于大型 xml 数据。

上面的链接为您提供了两者的示例。使用上述任何解析器从 xml 标记中解析和获取值。

于 2013-03-21T12:39:38.660 回答
0

您需要的只是一个 android xml 解析库。有很多用于 android 的 xml 解析。

官方教程

还有一篇文章《比较android中xml解析的方法

于 2013-03-21T13:29:52.007 回答
0

您可以尝试使用 SimpleXmlParser。这是一个原生的android类。它比 DOM xml 解析器更简单。

于 2013-03-21T12:39:08.117 回答