Java Code Examples for android.util.Xml#newPullParser()

The following examples show how to use android.util.Xml#newPullParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FeedParser.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
private static Feed beginParseStr2Feed(String xmlStr,String url) throws UnsupportedEncodingException {
    feedUrl = url;
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(new StringReader(xmlStr));

        while (parser.next() != XmlPullParser.END_TAG){
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals(RSS)){//RSS格式
                return readRssForFeed(parser);

            }else if (name.equals(FEED)){
                return readFeedForFeed(parser,url);
            }
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: FeedParser.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
private static Feed beginParseStr2Feed(String xmlStr,String url) throws UnsupportedEncodingException {
    feedUrl = url;
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(new StringReader(xmlStr));

        while (parser.next() != XmlPullParser.END_TAG){
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals(RSS)){//RSS格式
                return readRssForFeed(parser);

            }else if (name.equals(FEED)){
                return readFeedForFeed(parser,url);
            }
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: GlobalLanguageUtil.java    From Android with MIT License 5 votes vote down vote up
public void xmlParse(String code) throws XmlPullParserException, IOException {
    InputStream inputStream = BaseApplication.getInstance().getApplicationContext().getAssets().open("emotion_zh.xml");

    XmlPullParser pullParser = Xml.newPullParser();
    pullParser.setInput(inputStream, "UTF-8");

    String keyEn = "";
    String keyZh = "";
    int eventType = pullParser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                languageMap.clear();
                break;
            case XmlPullParser.START_TAG:
                keyEn = pullParser.getAttributeValue("", "name");
                pullParser.next();
                keyZh = pullParser.getText();
                if (!(TextUtils.isEmpty(keyEn) || TextUtils.isEmpty(keyZh))) {
                    if (code.contains("en")) {//ch to en
                        languageMap.put(keyZh, keyEn);
                    } else {//en to ch
                        languageMap.put(keyEn, keyZh);
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                break;
        }
        eventType = pullParser.next();
    }
}
 
Example 4
Source File: DLNANotifyParser.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
public JSONArray parse(InputStream in) throws XmlPullParserException, IOException, JSONException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readPropertySet(parser);
    } finally {
        in.close();
    }
}
 
Example 5
Source File: KmlParser.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static FileDataSource parse(InputStream in) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readKml(parser);
    } finally {
        in.close();
    }
}
 
Example 6
Source File: DLNAEventParser.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
public JSONObject parse(InputStream in) throws XmlPullParserException, IOException, JSONException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readEvent(parser);
    } finally {
        in.close();
    }
}
 
Example 7
Source File: XmlReader.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public XmlReader() {
    this.parser = Xml.newPullParser();
    try {
        this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    } catch (XmlPullParserException e) {
        Log.d(Config.LOGTAG, "error setting namespace feature on parser");
    }
}
 
Example 8
Source File: WeixinUtil.java    From android-common-utils with Apache License 2.0 5 votes vote down vote up
public static Map<String,String> decodeXml(String content) {
    try {
        Map<String, String> xml = new HashMap<>();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(new StringReader(content));
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {

            String nodeName=parser.getName();
            switch (event) {
                case XmlPullParser.START_DOCUMENT:

                    break;
                case XmlPullParser.START_TAG:
                    if(!"xml".equals(nodeName)){
                        //实例化student对象
                        xml.put(nodeName,parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
            }
            event = parser.next();
        }
        return xml;
    } catch (Exception e) {
        Log.e("orion",e.toString());
    }
    return null;

}
 
Example 9
Source File: AccountManagerBackupHelper.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public void restoreAccountAccessPermissions(byte[] data, int userId) {
    try {
        ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(dataStream, StandardCharsets.UTF_8.name());
        PackageManager packageManager = mAccountManagerService.mContext.getPackageManager();

        final int permissionsOuterDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, permissionsOuterDepth)) {
            if (!TAG_PERMISSIONS.equals(parser.getName())) {
                continue;
            }
            final int permissionOuterDepth = parser.getDepth();
            while (XmlUtils.nextElementWithin(parser, permissionOuterDepth)) {
                if (!TAG_PERMISSION.equals(parser.getName())) {
                    continue;
                }
                String accountDigest = parser.getAttributeValue(null, ATTR_ACCOUNT_SHA_256);
                if (TextUtils.isEmpty(accountDigest)) {
                    XmlUtils.skipCurrentTag(parser);
                }
                String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                if (TextUtils.isEmpty(packageName)) {
                    XmlUtils.skipCurrentTag(parser);
                }
                String digest =  parser.getAttributeValue(null, ATTR_DIGEST);
                if (TextUtils.isEmpty(digest)) {
                    XmlUtils.skipCurrentTag(parser);
                }

                PendingAppPermission pendingAppPermission = new PendingAppPermission(
                        accountDigest, packageName, digest, userId);

                if (!pendingAppPermission.apply(packageManager)) {
                    synchronized (mLock) {
                        // Start watching before add pending to avoid a missed signal
                        if (mRestorePackageMonitor == null) {
                            mRestorePackageMonitor = new RestorePackageMonitor();
                            mRestorePackageMonitor.register(mAccountManagerService.mContext,
                                    mAccountManagerService.mHandler.getLooper(), true);
                        }
                        if (mRestorePendingAppPermissions == null) {
                            mRestorePendingAppPermissions = new ArrayList<>();
                        }
                        mRestorePendingAppPermissions.add(pendingAppPermission);
                    }
                }
            }
        }

        // Make sure we eventually prune the in-memory pending restores
        mRestoreCancelCommand = new CancelRestoreCommand();
        mAccountManagerService.mHandler.postDelayed(mRestoreCancelCommand,
                PENDING_RESTORE_TIMEOUT_MILLIS);
    } catch (XmlPullParserException | IOException e) {
        Log.e(TAG, "Error restoring app permissions", e);
    }
}
 
Example 10
Source File: XmlPullFeedParser.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
public List<Message> parse() {
	List<Message> messages = null;
	XmlPullParser parser = Xml.newPullParser();
	try {
		// auto-detect the encoding from the stream
		parser.setInput(this.getInputStream(), null);
		int eventType = parser.getEventType();
		Message currentMessage = null;
		boolean done = false;
		while (eventType != XmlPullParser.END_DOCUMENT && !done){
			String name = null;
			switch (eventType){
				case XmlPullParser.START_DOCUMENT:
					messages = new ArrayList<Message>();
					break;
				case XmlPullParser.START_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase(ITEM)){
						currentMessage = new Message();
					} else if (currentMessage != null){
						if (name.equalsIgnoreCase(LINK)){
							currentMessage.setLink(parser.nextText());
						} else if (name.equalsIgnoreCase(DESCRIPTION)){
							currentMessage.setDescription(parser.nextText());
						} else if (name.equalsIgnoreCase(PUB_DATE)){
							currentMessage.setDate(parser.nextText());
						} else if (name.equalsIgnoreCase(TITLE)){
							currentMessage.setTitle(parser.nextText());
						}	
					}
					break;
				case XmlPullParser.END_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase(ITEM) && currentMessage != null){
						messages.add(currentMessage);
					} else if (name.equalsIgnoreCase(CHANNEL)){
						done = true;
					}
					break;
			}
			eventType = parser.next();
		}
	} catch (Exception e) {
		Log.e("AndroidNews::PullFeedParser", e.getMessage(), e);
		throw new RuntimeException(e);
	}
	return messages;
}
 
Example 11
Source File: XmlParser.java    From droidddle with Apache License 2.0 4 votes vote down vote up
/**
 * Read and parse res/raw/changelog.xml or custom file
 *
 * @throws Exception if changelog.xml or custom file is not found or if there are errors on parsing
 *
 * @return {@link ChangeLog} obj with all data
 */
@Override
public ChangeLog readChangeLogFile() throws Exception {

    ChangeLog chg = null;

    try {
        InputStream is = null;

        if (mChangeLogFileResourceUrl != null) {
            if (Util.isConnected(super.mContext)) {
                URL url = new URL(mChangeLogFileResourceUrl);
                is = url.openStream();
            }
        } else {
            is = mContext.getResources().openRawResource(mChangeLogFileResourceId);
        }
        if (is != null) {

            // Create a new XML Pull Parser.
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(is, null);
            parser.nextTag();

            // Create changelog obj that will contain all data
            chg = new ChangeLog();
            // Parse file
            readChangeLogNode(parser, chg);

            // Close inputstream
            is.close();
        } else {
            Log.d(TAG, "Changelog.xml not found");
            throw new ChangeLogException("Changelog.xml not found");
        }
    } catch (XmlPullParserException xpe) {
        Log.d(TAG, "XmlPullParseException while parsing changelog file", xpe);
        throw xpe;
    } catch (IOException ioe) {
        Log.d(TAG, "Error i/o with changelog.xml", ioe);
        throw ioe;
    }

    return chg;
}
 
Example 12
Source File: CssStyle.java    From 4pdaClient-plus with Apache License 2.0 4 votes vote down vote up
private static CssStyle parseStyle(CssStyle cssStyle, InputStream in) throws XmlPullParserException, IOException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    parser.setInput(in, null);


    int eventType = parser.getEventType();
    if (eventType == XmlPullParser.END_DOCUMENT)
        return null;

    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        eventType = parser.getEventType();

        if (eventType == XmlPullParser.START_TAG) {
            String name = parser.getName().toLowerCase();
            if (name.equals("title")) {
                parser.next();
                eventType = parser.getEventType();
                if (eventType == XmlPullParser.TEXT)
                    cssStyle.Title = parser.getText();
            } else if (name.equals("version")) {
                parser.next();
                eventType = parser.getEventType();
                if (eventType == XmlPullParser.TEXT)
                    cssStyle.Version = parser.getText();
            } else if (name.equals("author")) {
                parser.next();
                eventType = parser.getEventType();
                if (eventType == XmlPullParser.TEXT)
                    cssStyle.Author = parser.getText();
            } else if (name.equals("comment")) {
                parser.next();
                eventType = parser.getEventType();
                if (eventType == XmlPullParser.TEXT)
                    cssStyle.Comment = parser.getText();
            } else if (name.equals("screenshot")) {
                cssStyle.ScreenShots.add(parseScreenShot(parser));
            }

        }
    }
    return cssStyle;
}
 
Example 13
Source File: ParserXml.java    From Android-Basics-Codes with Artistic License 2.0 4 votes vote down vote up
/**
 * ���������н���Xml�ļ�
 * @param is
 * 			������
 * @return
 * 			null:��ʾ����Xml�ļ�����
 */
public static List<NewsBean> parserXmlFromStream(InputStream is){
	List<NewsBean> newsList = new ArrayList<NewsBean>();
	try {
		// 1. ��ʼ��Xml������
		XmlPullParser parser = Xml.newPullParser();
		// 2. ����������
		parser.setInput(is, "utf-8");
		// 3. ��������
		int type = parser.getEventType();
		NewsBean bean = null;
		
		while(type != XmlPullParser.END_DOCUMENT){
			//��ʼ���߽�����ǩ
			String tag = parser.getName();
			switch (type) {
			case XmlPullParser.START_TAG://��ʼ��ǩ
				if("item".equals(tag)){			//ÿ��������Ŀ�Ŀ�ʼ��ǩ
					bean  = new NewsBean();
				}else if("title".equals(tag)){	//���ŵı���
					String title = parser.nextText();
					bean.setTitle(title);
				}else if("des".equals(tag)){	//���ŵ�����
					String des = parser.nextText();
					bean.setDes(des);
				}else if("image".equals(tag)){	//���ŵ�ͼƬ��Url
					String imageUrl = parser.nextText();
					bean.setImage(imageUrl);
				}else if("comment".equals(tag)){//���ŵ�����
					String comment = parser.nextText();
					bean.setComment(comment);
				}
				break;
			case XmlPullParser.END_TAG://������ǩ
				if("item".equals(tag)){			//ÿ��������Ŀ�Ľ�����ǩ
					newsList.add(bean);
				}
				break;

			default:
				break;
			}
			
			//�õ���һ���¼�
			type = parser.next();
		}
		
		return newsList;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 14
Source File: ParserXml.java    From Android-Basics-Codes with Artistic License 2.0 4 votes vote down vote up
/**
 * ���������н���Xml�ļ�
 * @param is
 * 			������
 * @return
 * 			null:��ʾ����Xml�ļ�����
 */
public static List<NewsBean> parserXmlFromStream(InputStream is){
	List<NewsBean> newsList = new ArrayList<NewsBean>();
	try {
		// 1. ��ʼ��Xml������
		XmlPullParser parser = Xml.newPullParser();
		// 2. ����������
		parser.setInput(is, "utf-8");
		// 3. ��������
		int type = parser.getEventType();
		NewsBean bean = null;
		
		while(type != XmlPullParser.END_DOCUMENT){
			//��ʼ���߽�����ǩ
			String tag = parser.getName();
			switch (type) {
			case XmlPullParser.START_TAG://��ʼ��ǩ
				if("item".equals(tag)){			//ÿ��������Ŀ�Ŀ�ʼ��ǩ
					bean  = new NewsBean();
				}else if("title".equals(tag)){	//���ŵı���
					String title = parser.nextText();
					bean.setTitle(title);
				}else if("des".equals(tag)){	//���ŵ�����
					String des = parser.nextText();
					bean.setDes(des);
				}else if("image".equals(tag)){	//���ŵ�ͼƬ��Url
					String imageUrl = parser.nextText();
					bean.setImage(imageUrl);
				}else if("comment".equals(tag)){//���ŵ�����
					String comment = parser.nextText();
					bean.setComment(comment);
				}
				break;
			case XmlPullParser.END_TAG://������ǩ
				if("item".equals(tag)){			//ÿ��������Ŀ�Ľ�����ǩ
					newsList.add(bean);
				}
				break;

			default:
				break;
			}
			
			//�õ���һ���¼�
			type = parser.next();
		}
		
		return newsList;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 15
Source File: XmlUtils.java    From XPrivacy with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Read an ArrayList from an InputStream containing XML.  The stream can
 * previously have been written by writeListXml().
 *
 * @param in The InputStream from which to read.
 *
 * @return ArrayList The resulting list.
 *
 * @see #readMapXml
 * @see #readValueXml
 * @see #readThisListXml
 * @see #writeListXml
 */
public static final ArrayList readListXml(InputStream in)
throws XmlPullParserException, java.io.IOException
{
    XmlPullParser   parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (ArrayList)readValueXml(parser, new String[1]);
}
 
Example 16
Source File: XmlUtils.java    From TowerCollector with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Read an ArrayList from an InputStream containing XML.  The stream can
 * previously have been written by writeListXml().
 *
 * @param in The InputStream from which to read.
 *
 * @return ArrayList The resulting list.
 *
 * @see #readMapXml
 * @see #readValueXml
 * @see #readThisListXml
 * @see #writeListXml
 */
public static final ArrayList readListXml(InputStream in)
        throws XmlPullParserException, java.io.IOException
{
    XmlPullParser   parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (ArrayList)readValueXml(parser, new String[1]);
}
 
Example 17
Source File: XmlUtils.java    From HtmlCompat with Apache License 2.0 3 votes vote down vote up
/**
 * Read an ArrayList from an InputStream containing XML.  The stream can
 * previously have been written by writeListXml().
 *
 * @param in The InputStream from which to read.
 *
 * @return ArrayList The resulting list.
 *
 * @see #readMapXml
 * @see #readValueXml
 * @see #readThisListXml
 * @see #writeListXml
 */
public static final ArrayList readListXml(InputStream in)
        throws XmlPullParserException, java.io.IOException
{
    XmlPullParser   parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (ArrayList)readValueXml(parser, new String[1]);
}
 
Example 18
Source File: XmlUtils.java    From HeadsUp with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Read an ArrayList from an InputStream containing XML.  The stream can
 * previously have been written by writeListXml().
 *
 * @param in The InputStream from which to read.
 * @return ArrayList The resulting list.
 * @see #readMapXml
 * @see #readValueXml
 * @see #readThisListXml
 * @see #writeListXml
 */
public static ArrayList readListXml(InputStream in)
        throws XmlPullParserException, java.io.IOException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (ArrayList) readValueXml(parser, new String[1]);
}
 
Example 19
Source File: XmlUtils.java    From J2ME-Loader with Apache License 2.0 3 votes vote down vote up
/**
 * Read a HashMap from an InputStream containing XML.  The stream can
 * previously have been written by writeMapXml().
 *
 * @param in The InputStream from which to read.
 * @return HashMap The resulting map.
 * @see #readListXml
 * @see #readValueXml
 * @see #readThisMapXml
 * #see #writeMapXml
 */
@SuppressWarnings("unchecked")
public static final HashMap<String, Object> readMapXml(InputStream in)
		throws XmlPullParserException, java.io.IOException {
	XmlPullParser parser = Xml.newPullParser();
	parser.setInput(in, "UTF-8");
	return (HashMap<String, Object>) readValueXml(parser, new String[1]);
}
 
Example 20
Source File: XmlUtils.java    From Android-PreferencesManager with Apache License 2.0 2 votes vote down vote up
/**
 * Read a HashMap from an InputStream containing XML. The stream can
 * previously have been written by writeMapXml().
 *
 * @param in The InputStream from which to read.
 * @return HashMap The resulting map.
 * @see #readListXml
 * @see #readValueXml
 * @see #readThisMapXml #see #writeMapXml
 */
public static final HashMap readMapXml(InputStream in) throws XmlPullParserException, java.io.IOException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (HashMap) readValueXml(parser, new String[1]);
}