码迷,mamicode.com
首页 > 编程语言 > 详细

Java解析Xml格式的响应信息

时间:2020-01-13 12:31:15      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:http   cut   line   attr   sage   地址   信息   error   password   

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;
    }

Java解析Xml格式的响应信息

标签:http   cut   line   attr   sage   地址   信息   error   password   

原文地址:https://www.cnblogs.com/miniDog/p/12186469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!