如何通过URL将XML数据提取到字符串

时间:2017-08-14 11:36:13

标签: java android xml string parsing

我想使用此URL将xml数据从library转换为json,但它不会处理来自URL的xml ..!只有它是一个字符串或文件,所以我想将url中的数据转换为字符串!

有可能吗?

1 个答案:

答案 0 :(得分:1)

此片段可以帮助您

 new Thread() {
        public void run() {
            URL url = null;
            BufferedReader in = null;
            try {
                url = new URL("your url");

                in = new BufferedReader(
                        new InputStreamReader(
                                url.openStream(),"UTF-8"));//in most cases there is utf 8

                String inputLine;
                StringBuilder builder = new StringBuilder();
                while ((inputLine = in.readLine()) != null)
                    builder.append(inputLine);
                String urlContent = builder.toString();
                // process your received data somehow
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();