解析xml

来源:互联网 发布:山东财经大学网络缴费 编辑:程序博客网 时间:2024/03/29 21:15

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
 {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
 
   String domainmsg=null;
         URL url = null;
         BufferedReader in = null;
         InputStreamReader isr = null;
         InputStream is = null;
         HttpURLConnection huc = null;
         try
         {
             //url = new URL("http://now.net.cn/domain/domaincheck.php?query="+param);
             url = new URL("http://www.waniba.com/interface/chl/getallsort.ashx");
            
             huc = (HttpURLConnection)url.openConnection();
          
             is = huc.getInputStream();
             isr = new InputStreamReader(is);
             in = new BufferedReader(isr);
             String line = null;
             StringBuffer s = new StringBuffer(333);
             while((line = in.readLine()) != null) {
                 line=line.trim();
                 s.append(line);
             }
             domainmsg=s.toString();
             System.out.println(domainmsg);
             SAXBuilder builder = new SAXBuilder();
             Document doc = null;
             Reader in1= new StringReader(domainmsg);
             doc = builder.build(in1);
             Element root = doc.getRootElement();
            //System.out.println(doc);
            
            //List ls = root.getChildren();//注意此处取出的是root节点下面的一层的Element集合
            List ls = (List)root.getChildren("WidgetSortList");
           
           
            for (Iterator iter = ls.iterator(); iter.hasNext(); ) {
             
                Element el = (Element) iter.next();
                //System.out.println( el.getAttributeValue("sort"));
              
                 List elList = (List) el.getChildren();
              
                for(int i=0;i<elList.size();i++)
                {
                    Element ell = (Element)elList.get(i) ;
                    System.out.println(ell.getAttributeValue("id"));
                }
                /*getAttributeValue("width");//获得attribute
                 * 类别查询
                 */
                /*if(el.getName().equals("WidgetSortList")){
                 System.out.println(el.getText()+"a");
                }*/
            }
             // String ss=root.getChild("WidgetSortList").getChildText("sort");
             // System.out.println(ls );
         }catch(Exception e){
            e.printStackTrace();
         }
         finally
         {
             try
             {
                 huc.disconnect();
                 is.close();
                 isr.close();
                 in.close();
             }
             catch(Exception e)
             {
                e.printStackTrace();
             }
         }
        
  
  
  out.flush();
  out.close();
 }

 

 

public static InputStream down()
 {
  
   URL url;
   URLConnection urlc;
   InputStream is = null;
   try {
    url = new URL("ftp://user01:pass1234@ftp.foo.com/README.txt;type=i");
    urlc = url.openConnection();
    is= urlc.getInputStream();//下载
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return is;
 }