0

I have this all set up to get some xml from the web and parse it and its working, but I am wondering where I can log the xml/ save it to a file so i can view what is being downloaded just to check it is what I want. Here is my set up. And I have tried logging receivedData and xmlData everywhere...?

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;
    }

And then in my onCreate I call

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

How can I view the xml that I have downloaded?

Thanks for the help :)

iqueqiorio
  • 989
  • 2
  • 30
  • 73

1 Answers1

0

Change your result stream to string and you are done to log it.

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

Reference

Community
  • 1
  • 1
Darpan
  • 5,193
  • 3
  • 45
  • 74