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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM