Example usage for android.util Xml newPullParser

List of usage examples for android.util Xml newPullParser

Introduction

In this page you can find the example usage for android.util Xml newPullParser.

Prototype

public static XmlPullParser newPullParser() 

Source Link

Document

Returns a new pull parser with namespace support.

Usage

From source file:android.support.v7.widget.ActivityChooserModel.java

private void readHistoricalDataImpl() {
    FileInputStream fis = null;/*from   w w w  .j a v  a 2s . c o  m*/
    try {
        fis = mContext.openFileInput(mHistoryFileName);
    } catch (FileNotFoundException fnfe) {
        if (DEBUG) {
            Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
        }
        return;
    }
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, "UTF-8");

        int type = XmlPullParser.START_DOCUMENT;
        while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
            type = parser.next();
        }

        if (!TAG_HISTORICAL_RECORDS.equals(parser.getName())) {
            throw new XmlPullParserException(
                    "Share records file does not start with " + TAG_HISTORICAL_RECORDS + " tag.");
        }

        List<HistoricalRecord> historicalRecords = mHistoricalRecords;
        historicalRecords.clear();

        while (true) {
            type = parser.next();
            if (type == XmlPullParser.END_DOCUMENT) {
                break;
            }
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }
            String nodeName = parser.getName();
            if (!TAG_HISTORICAL_RECORD.equals(nodeName)) {
                throw new XmlPullParserException("Share records file not well-formed.");
            }

            String activity = parser.getAttributeValue(null, ATTRIBUTE_ACTIVITY);
            final long time = Long.parseLong(parser.getAttributeValue(null, ATTRIBUTE_TIME));
            final float weight = Float.parseFloat(parser.getAttributeValue(null, ATTRIBUTE_WEIGHT));
            HistoricalRecord readRecord = new HistoricalRecord(activity, time, weight);
            historicalRecords.add(readRecord);

            if (DEBUG) {
                Log.i(LOG_TAG, "Read " + readRecord.toString());
            }
        }

        if (DEBUG) {
            Log.i(LOG_TAG, "Read " + historicalRecords.size() + " historical records.");
        }
    } catch (XmlPullParserException xppe) {
        Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, xppe);
    } catch (IOException ioe) {
        Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, ioe);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ioe) {
                /* ignore */
            }
        }
    }
}

From source file:com.connectsdk.service.DLNAService.java

String parseData(String response, String key) {
    if (isXmlEncoded(response)) {
        response = Html.fromHtml(response).toString();
    }/*from   w  ww .j  ava 2  s.co  m*/
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(new StringReader(response));
        int event;
        boolean isFound = false;
        do {
            event = parser.next();
            if (event == XmlPullParser.START_TAG) {
                String tag = parser.getName();
                if (key.equals(tag)) {
                    isFound = true;
                }
            } else if (event == XmlPullParser.TEXT && isFound) {
                return parser.getText();
            }
        } while (event != XmlPullParser.END_DOCUMENT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.ridgelineapps.wallpaper.photosite.FlickrUtils.java

/**
 * Parses a valid Flickr XML response from the specified input stream. When the Flickr
 * response contains the OK tag, the response is sent to the specified response parser.
 *
 * @param in The input stream containing the response sent by Flickr.
 * @param responseParser The parser to use when the response is valid.
 * //  w w  w .j  a v  a  2 s .co m
 * @throws IOException
 */
private void parseResponse(InputStream in, ResponseParser responseParser) throws IOException {
    final XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(new InputStreamReader(in));

        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty
        }

        if (type != XmlPullParser.START_TAG) {
            throw new InflateException(parser.getPositionDescription() + ": No start tag found!");
        }

        String name = parser.getName();
        if (RESPONSE_TAG_RSP.equals(name)) {
            final String value = parser.getAttributeValue(null, RESPONSE_ATTR_STAT);
            if (!RESPONSE_STATUS_OK.equals(value)) {
                throw new IOException("Wrong status: " + value);
            }
        }

        responseParser.parseResponse(parser);

    } catch (XmlPullParserException e) {
        final IOException ioe = new IOException("Could not parse the response");
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:com.com.easemob.chatuidemo.adapter.MessageAdapter.java

/**
 * ??//w w  w. j a  v  a 2 s. co  m
 * @param xml  InputStream
 * @return UserInfoBean
 * @throws Exception
 */
private UserInfoBean getUserInf(InputStream xml) throws Exception {

    UserInfoBean userInf = null;
    XmlPullParser parser = Xml.newPullParser(); // AndroidXml?xmlPull?
    parser.setInput(xml, "UTF-8"); // ?
    int event = parser.getEventType(); // ???int?
    while (event != XmlPullParser.END_DOCUMENT) {
        switch (event) {

        case XmlPullParser.START_TAG:
            if ("Document".equals(parser.getName())) {
                userInf = new UserInfoBean();
            } else if ("name".equals(parser.getName())) {
                userInf.setName(parser.nextText());
            } else if ("number".equals(parser.getName())) {
                userInf.setNumber(parser.nextText());
            } else if ("sex".equals(parser.getName())) {
                userInf.setSex(parser.nextText());
            } else if ("birthday".equals(parser.getName())) {
                userInf.setBirthday(parser.nextText());
            } else if ("school".equals(parser.getName())) {
                userInf.setSchool(parser.nextText());
            } else if ("hometown".equals(parser.getName())) {
                userInf.setHomeTown(parser.nextText());
            } else if ("province".equals(parser.getName())) {
                userInf.setProvince(parser.nextText());
            } else if ("habit".equals(parser.getName())) {
                userInf.setHabit(parser.nextText());
            } else if ("job".equals(parser.getName())) {
                userInf.setJob(parser.nextText());
            } else if ("headImg".equals(parser.getName())) {
                userInf.setHeadImg(parser.nextText());
            } else if ("photos".equals(parser.getName())) {
                userInf.setPhotos(parser.nextText());
            }
            break;
        case XmlPullParser.END_TAG:
            break;
        }
        event = parser.next(); // ?
    }
    return userInf;
}