zoukankan      html  css  js  c++  java
  • Java解析Xml格式的响应信息

    1.请求/响应代码

        public List<ZxAjxxListItem> getZxAjxxList() {
            
            List<ZxAjxxListItem> r = null;
            if (login()) {
                HttpGet get = null;
                try {
              // 请求资源地址 String url
    =clientAccountAndPassword.getAjcxGridUrl(); get = new HttpGet(url);
              // 响应信息 HttpResponse response
    = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == 200) {
                // 响应资源内容 String responseXml
    = EntityUtils.toString(response.getEntity(), "UTF-8"); r = getXmltoZxAjxxListItem(responseXml); } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (get != null) { get.abort(); } } } return r; }

    2.解析响应xml格式信息代码

        private List<ZxAjxxListItem> getXmltoZxAjxxListItem(String xmlString) {
            if(StringUtils.isBlank(xmlString)) return null;
            Document doc = null;
            List<ZxAjxxListItem> zxAjxxListItem = null;
            try {
                // 将字符串转为XML
                doc = DocumentHelper.parseText(xmlString);
                // 获取根节点
                Element rootElt = doc.getRootElement();
                // 获取row节点
                zxAjxxListItem = new ArrayList<ZxAjxxListItem>();
                Iterator rowList = rootElt.elementIterator("row");
                ZxAjxxListItem bean = null;
                while (rowList.hasNext()) {
                    bean = new ZxAjxxListItem();
                    Element row = (Element) rowList.next();
                    bean .setAhdm(row.attributeValue("id"));
                    // 获取cell节点
                    Iterator cellList = row.elementIterator("cell");
                    List<String> strlist = new ArrayList<String>();
                    while (cellList.hasNext()) {
                        Element cell = (Element) cellList.next();
                        strlist.add(cell.getText());
                    }
                    bean.setAjzt(strlist.get(2));
                    bean.setXlabh(strlist.get(3));
                    bean.setAh(strlist.get(4));
                    bean.setDsr(strlist.get(5));
                    bean.setAy(strlist.get(6));
                    zxAjxxListItem.add(bean);
                }
                return zxAjxxListItem;
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            return zxAjxxListItem;
        }
  • 相关阅读:
    layui iframe版点击左侧菜单栏实现加载等待动画
    概率论基础内容
    fatal error LNK1123:转换COFF期间失败:文件无效或损坏
    ERROR 2003:Can't connect to MySQL server on ‘localhost’...
    HttpWebRequest 无法连接到远程服务器
    Android Error: java.lang.IllegalStateException: Could not execute method of the activity
    创建.aspx页面
    CodeFile Inherits
    Unable to execute dex:Target out of range
    fatal error C1083: 无法打开包括文件“jni.h”
  • 原文地址:https://www.cnblogs.com/miniDog/p/12186469.html
Copyright © 2011-2022 走看看