EXTJS AJAX解析XML数据

public String getAllAreaInfos() {  
        try {  
            List<Areainfo> list = null;  
            if(areaName!=null&&!areaName.equalsIgnoreCase("null")&&areaName.trim().length()>0)  
            {  
                list = mapInfos.findAllAreaInfos(this.subStringAreaName(areaName));  
            }  
            else  
                list = mapInfos.findAllAreaInfos(null);  
                  
            response.setContentType("text/xml;charset=utf-8");  
            response.setCharacterEncoding("utf-8");  
            PrintWriter pw = null;  
            pw = response.getWriter();  
            StringBuffer sb = new StringBuffer();  
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");  
              
            sb.append("<Items>");  
            if(list==null)  
            {  
                sb.append("<Areainfo>");  
                sb.append("<id>").append("no").append("</id>");  
                sb.append("</Areainfo>");  
            }  
            else  
            {  
                for (int i = 0; i < list.size(); i++)   
                {  
                    Areainfo v = list.get(i);  
                    sb.append("<Areainfo>");  
                      
                    sb.append("<id>").append(v.getId()).append("</id>");  
                    sb.append("<name>").append(v.getName()).append("</name>");  
                    sb.append("<allCapacity>").append(v.getAllcapacity()).append("</allCapacity>");  
                      
                    sb.append("</Areainfo>");  
                }  
            }  
            sb.append("</Items>");  
              
            String xmlDBTree = sb.toString();  
              
            pw.print(xmlDBTree);  
            pw.flush();  
            pw.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    } 
<?xml version="1.0" encoding="UTF-8"?>"
<Items>
    <Areainfo>
    <id>xxx</id>
    <name>xxxx</name>
    <allCapacity>xxxx</allCapacity>
    </Areainfo>
</Items>
function getAllAreaInfos()  
    {  
        var areaName = "";  
        Ext.Ajax.request({  
            method: 'POST',  
            url: 'allAreaInfos',  
            params: {"areaName":areaName},  
            waitMsg: '数据更新中,请稍后...',  
            success: function(response,options){  
                var xmlDoc = response.responseXML;  
                if(xmlDoc!=null)  
                {  
                    var ais = xmlDoc.getElementsByTagName("Areainfo");   
                    if(ais!=null&&ais.length>0)   
                    {  
                        for(var i=0;i<ais.length;i++)//   DB    
                        {    
                            var asId = (ais[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);    //  第i个areaInfos的id    
                            var asName = (ais[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);    //  第i个areaInfos的名字    
                            var allCapacity = (ais[i].getElementsByTagName("allCapacity")[0].childNodes[0].nodeValue);    //  第i个areaInfos的总装机容量  
                            //document.getElementById(asName).innerHTML="";  
                            document.getElementById(asName).innerHTML=allCapacity;//得到的数据在页面上展示  
                        }    
                    }  
                }  
            },  
            failure: function(response,options){  
                Ext.Msg.alert("信息提示", "获取数据库数据出错,请稍后再试!");  
            },  
            autoAbort:false  
        });  
    } 

 

posted @ 2013-10-29 13:14  牧之丨  阅读(618)  评论(0编辑  收藏  举报