首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XML解析 - 转载

2012-08-22 
XML解析 -- 转载//连接服务器public static InputStream openConn(String path) {HttpURLConnection ucUR

XML解析 -- 转载
//连接服务器
    public static InputStream openConn(String path) {
        HttpURLConnection uc;
        URL url;
        InputStream is = null;
        try {
            url = new URL(path);
            // SocketAddress addr = new
            // InetSocketAddress("10.0.0.172",80);//是代理地址
            // Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);
            // uc = (HttpURLConnection) url.openConnection(typeProxy);
            uc = (HttpURLConnection) url.openConnection();
            uc.connect();
            is = uc.getInputStream();
            //uc.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
       
        return is;
    }


//XML解析部分
        InputStream is = openConn(你要解析的服务器的页面);
       
        //无法连接
        if (is == null)
            return false;
       
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return false;
        }
        Document document = null;
        try {
            document = builder.parse(is);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

        Element rootElement = document.getDocumentElement();

        NodeList list = rootElement.getElementsByTagName("你的XML节点名");

热点排行