XmlPullParser日志下载的xml

时间:2014-12-11 06:37:24

标签: java android xml xmlpullparser

我已将此设置为从网络获取一些xml并解析它及其工作,但我想知道我可以在哪里登录xml /将其保存到文件中以便我可以查看正在下载的内容以便检查这就是我想要的。这是我的设置。我尝试在任何地方记录receivedData和xmlData ......?

private class AsyncDownloader extends AsyncTask<Object, String, Integer> {

    @Override
    protected Integer doInBackground(Object... arg0) {
        XmlPullParser receivedData = tryDownloadingXmlData();
        int recordsFound = tryParsingXmlData(receivedData);
        return recordsFound;
    }


    private XmlPullParser tryDownloadingXmlData() {
        try {
            URL xmlUrl = new URL(url);
            XmlPullParser receivedData = XmlPullParserFactory.newInstance().newPullParser();
            receivedData.setInput(xmlUrl.openStream(), null);
            return receivedData;
        } catch (XmlPullParserException e) {
            Log.i("tag1", e + "");
        } catch (IOException e) {
            Log.i("tag1", e + "");
        }

        return null;
    }

    private int tryParsingXmlData(XmlPullParser receivedData) {
        if (receivedData != null) {
            try {
                return processReceivedData(receivedData);
            } catch (XmlPullParserException e) {
                Log.e(TAG, "Pull Parser failure", e);
            } catch (IOException e) {
                Log.e(TAG, "IO Exception parsing XML", e);
            }
        }
        return 0;
    }

        private int processReceivedData(XmlPullParser xmlData) throws XmlPullParserException, IOException {

        int eventType = -1;
        int recordsFound = 0;

        String appId = "";
        String itemId = "";
        String timeStamp = "";
        String data = "";
        String day = "";

        Log.i(TAG, xmlData + "");


        while (eventType != XmlResourceParser.END_DOCUMENT) {
            String tagName = xmlData.getName();

            switch (eventType) {
                case XmlResourceParser.START_TAG:
                    if (tagName.equalsIgnoreCase("day")) {
                        day = xmlData.getAttributeValue(null, "name");

                    }

                    break;
                case XmlResourceParser.TEXT:
                    data += xmlData.getText();
                    break;
                case XmlPullParser.END_TAG:
                    if (tagName.equals("day")) {
                        recordsFound++;
                        publishProgress(day);
                    }
                    break;
            }
            eventType = xmlData.next();

        }

        if (recordsFound == 0) {
            publishProgress();
        }
        return 0;
    }

然后在我的onCreate中我打电话

AsyncDownloader downloader = new AsyncDownloader();
    downloader.execute();

如何查看已下载的xml?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

将结果流更改为字符串,然后完成记录。

java.util.Scanner s = new java.util.Scanner(xmlUrl.openStream()).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : ""    
Log.d("TAG","result " + result);

Reference