Android4.0使用dom解析

来源:互联网 发布:mac怎么关闭加密文件夹 编辑:程序博客网 时间:2024/06/01 10:36
SDomParese.getWeather(url2);public static void getWeather(String uri) {try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();InputStream is = HttpUtils.getInputStremFromXml(uri, "utf-8");Document document = builder.parse(is);Element root = document.getDocumentElement();NodeList list = root.getElementsByTagName("s");for (int i = 0; i < list.getLength(); i++) {Element element = (Element) list.item(i);GlobalConstant.i("k--->>" + element.getAttribute("k"));GlobalConstant.i("d--->>" + element.getAttribute("d"));}} catch (Exception e) {GlobalConstant.i("dom parse exception-->" + e);e.printStackTrace();}}
/** *  * 返回inputstream类型的xml数据 *  * @param path * @param encode * @return */public static InputStream getInputStremFromXml(String path, String encode) {HttpClient httpClient = new DefaultHttpClient();try {HttpPost httpPost = new HttpPost(path);HttpResponse httpResponse = httpClient.execute(httpPost);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity httpEntity = httpResponse.getEntity();return httpEntity.getContent();}} catch (Exception e) {System.out.println("get stream exception--->" + e);e.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}return null;}


0 0
原创粉丝点击