Java Code Examples for org.xmlpull.v1.XmlPullParser#setFeature()

The following examples show how to use org.xmlpull.v1.XmlPullParser#setFeature() . 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: BluetoothXmlParser.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
public HashMap<UUID, Service> parseServices() throws XmlPullParserException, IOException {
    String[] serviceFiles = appContext.getAssets().list(Consts.DIR_SERVICE);
    InputStream in = null;
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    HashMap<UUID, Service> services = new HashMap<>();
    for (String fileName : serviceFiles) {
        try {
            in = appContext.getAssets().open(Consts.DIR_SERVICE + File.separator + fileName);
            parser.setInput(in, null);
            parser.nextTag();
            UUID uuid = readUUID(parser);
            Service service = readService(parser);
            service.setUuid(uuid);
            services.put(uuid, service);
            in.close();
        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
    return services;
}
 
Example 4
Source File: ApplicationListParser.java    From Stringlate with MIT License 6 votes vote down vote up
static ArrayList<ApplicationDetails> parseFromXml(InputStream in, HashSet<String> installedPackages)
        throws XmlPullParserException, IOException {

    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();

        ArrayList<ApplicationDetails> apps = readFdroid(parser);
        // Now set which applications are installed on the device
        for (ApplicationDetails app : apps) {
            if (installedPackages.contains(app.getPackageName()))
                app.setInstalled();
        }

        return apps;
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
}
 
Example 5
Source File: FreeOtpImporter.java    From Aegis with GNU General Public License v3.0 6 votes vote down vote up
@Override
public State read(FileReader reader) throws DatabaseImporterException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(reader.getStream(), null);
        parser.nextTag();

        List<JSONObject> entries = new ArrayList<>();
        for (PreferenceParser.XmlEntry entry : PreferenceParser.parse(parser)) {
            if (!entry.Name.equals("tokenOrder")) {
                entries.add(new JSONObject(entry.Value));
            }
        }
        return new State(entries);
    } catch (XmlPullParserException | IOException | JSONException e) {
        throw new DatabaseImporterException(e);
    }
}
 
Example 6
Source File: SimpleXmlParser.java    From ssj with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param in               InputStream
 * @param searchPath       String[]
 * @param searchAttributes String[]
 * @return XmlValues
 * @throws XmlPullParserException
 * @throws IOException
 */
public XmlValues parse(InputStream in, String[] searchPath, String[] searchAttributes) throws XmlPullParserException, IOException
{
    this.searchPath = searchPath;
    this.searchAttributes = searchAttributes;
    xmlValues = new XmlValues();
    try
    {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        search(parser, 0);
        return xmlValues;
    } finally
    {
        in.close();
    }
}
 
Example 7
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 8
Source File: WISPAccessGatewayParam.java    From WiFiAfterConnect with Apache License 2.0 5 votes vote down vote up
public static WISPAccessGatewayParam parse (InputStream in) throws IOException {
	try {
           XmlPullParser parser = Xml.newPullParser();
           parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
           parser.setInput(in, null);
		parser.nextTag();
           return parse(parser);
       } catch (XmlPullParserException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 9
Source File: ResourcesParser.java    From Stringlate with MIT License 5 votes vote down vote up
static void loadFromXml(final InputStream in, final Resources resources, final XmlPullParser parser)
        throws XmlPullParserException, IOException {

    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        readResourcesInto(parser, resources);
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
}
 
Example 10
Source File: TotpAuthenticatorImporter.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
@Override
public State read(FileReader reader) throws DatabaseImporterException {
    try {
        if (reader.isInternal()) {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(reader.getStream(), null);
            parser.nextTag();

            String data = null;
            for (PreferenceParser.XmlEntry entry : PreferenceParser.parse(parser)) {
                if (entry.Name.equals(PREF_KEY)) {
                    data = entry.Value;
                }
            }

            if (data == null) {
                throw new DatabaseImporterException(String.format("Key %s not found in shared preference file", PREF_KEY));
            }

            List<JSONObject> entries = parse(data);
            return new DecryptedState(entries);
        } else {
            byte[] base64 = reader.readAll();
            byte[] cipherText = Base64.decode(base64);
            return new EncryptedState(cipherText);
        }
    } catch (IOException | XmlPullParserException | JSONException e) {
        throw new DatabaseImporterException(e);
    }
}
 
Example 11
Source File: YahooXmlParser.java    From Equate with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse a stream of XML data and extract currency rates and times.
 * @param stream input XML stream to parse
 * @return a HashMap of all the symbols and prices from the XML stream
 * @throws CurrencyParseException if there is any problem parsing the stream
 */
@Override
protected HashMap<String, Entry> parse(InputStream stream) throws CurrencyParseException {
	try {
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		parser.setInput(stream, null);
		parser.nextTag();
		return findResources(parser);
	} catch (IOException | XmlPullParserException e) {
		throw new CurrencyParseException(e.getMessage());
	}
}
 
Example 12
Source File: GPXReader.java    From FXMaps with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public XmlPullParser createParser() {
    try {
        final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        return parser;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: PListParser.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
public JSONObject parse(String text) throws XmlPullParserException, IOException,
        JSONException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    Reader stream = new StringReader(text);
    parser.setInput(stream);
    parser.nextTag();
    return readPlist(parser);
}
 
Example 14
Source File: StackOverflowXmlParser.java    From AIMSICDL with GNU General Public License v3.0 5 votes vote down vote up
public List<Cell> 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 readCells(parser);
    } finally {
        in.close();
    }
}
 
Example 15
Source File: BluetoothXmlParser.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
public HashMap<UUID, Descriptor> parseDescriptors() throws IOException, XmlPullParserException {

        String[] descriptorsFiles = appContext.getAssets().list(Consts.DIR_DESCRIPTOR);
        InputStream in = null;
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        HashMap<UUID, Descriptor> descriptors = new HashMap<>();

        for (String fileName : descriptorsFiles) {
            try {
                in = appContext.getAssets().open(Consts.DIR_DESCRIPTOR + File.separator + fileName);
                parser.setInput(in, null);
                parser.nextTag();
                UUID uuid = readUUID(parser);
                Descriptor descriptor = readDescriptor(parser);
                descriptor.setUuid(uuid);
                descriptors.put(uuid, descriptor);
                in.close();
            } catch (XmlPullParserException | IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
        return descriptors;
    }
 
Example 16
Source File: ECBXmlParser.java    From Equate with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse a stream of XML data and extract currency rates and times.
 * @param stream input XML stream to parse
 * @return a HashMap of all the symbols and prices from the XML stream
 * @throws CurrencyParseException if there is any problem parsing the stream
 */
@Override
protected HashMap<String, Entry> parse(InputStream stream) throws CurrencyParseException {
	try {
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		parser.setInput(stream, null);
		parser.nextTag();
		HashMap<String, Entry> euroBased =  findSecondElement(parser);
		return convertEuroToDollar(euroBased);
	} catch (IOException | XmlPullParserException e) {
		throw new CurrencyParseException(e.getMessage());
	}
}
 
Example 17
Source File: Model.java    From ssj with GNU General Public License v3.0 4 votes vote down vote up
private void parseTrainerFile(File file) throws XmlPullParserException, IOException, SSJException
{
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    parser.setInput(new FileReader(file));

    parser.next();
    if (parser.getEventType() != XmlPullParser.START_TAG || !parser.getName().equalsIgnoreCase("trainer"))
    {
        Log.w("unknown or malformed trainer file");
        return;
    }

    ArrayList<String> classNamesList = new ArrayList<>();

    while (parser.next() != XmlPullParser.END_DOCUMENT) {

        //STREAM
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("streams")) {

            parser.nextTag(); //item
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("item")) {

                input_bytes = Integer.valueOf(parser.getAttributeValue(null, "byte"));
                input_dim = Integer.valueOf(parser.getAttributeValue(null, "dim"));
                input_sr = Float.valueOf(parser.getAttributeValue(null, "sr"));
                input_type = Cons.Type.valueOf(parser.getAttributeValue(null, "type"));
            }
        }

        // CLASS
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("classes"))
        {
            parser.nextTag();

            while (parser.getName().equalsIgnoreCase("item"))
            {
                if (parser.getEventType() == XmlPullParser.START_TAG)
                {
                    classNamesList.add(parser.getAttributeValue(null, "name"));
                }
                parser.nextTag();
            }
        }

        //SELECT
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("select")) {

            parser.nextTag(); //item
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("item")) {

                int stream_id = Integer.valueOf(parser.getAttributeValue(null, "stream"));
                if (stream_id != 0)
                    Log.w("multiple input streams not supported");
                String[] select = parser.getAttributeValue(null, "select").split(" ");
                select_dimensions = new int[select.length];
                for (int i = 0; i < select.length; i++) {
                    select_dimensions[i] = Integer.valueOf(select[i]);
                }
            }
        }

        //MODEL
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("model"))
        {
            String expectedModel = parser.getAttributeValue(null, "create");
            if(!_name.equals(expectedModel))
                Log.w("trainer file demands a " + expectedModel + " model, we provide a " + _name + " model.");

            modelFileName = parser.getAttributeValue(null, "path");
            modelOptionFileName =  parser.getAttributeValue(null, "option");

            if (modelFileName != null && modelFileName.endsWith("." + FileCons.FILE_EXTENSION_MODEL))
            {
                modelFileName = modelFileName.replaceFirst("(.*)\\." + FileCons.FILE_EXTENSION_MODEL + "$", "$1");
            }

            if (modelOptionFileName != null && modelOptionFileName.endsWith("." + FileCons.FILE_EXTENSION_OPTION))
            {
                modelOptionFileName = modelOptionFileName.replaceFirst("(.*)\\." + FileCons.FILE_EXTENSION_OPTION + "$", "$1");
            }
        }

        if (parser.getEventType() == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("trainer"))
            break;
    }

    class_names = classNamesList.toArray(new String[0]);
    n_classes = class_names.length;
}
 
Example 18
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 19
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 20
Source File: PreferenceParser.java    From SearchPreference with MIT License 4 votes vote down vote up
private ArrayList<PreferenceItem> parseFile(SearchConfiguration.SearchIndexItem item) {
    java.util.ArrayList<PreferenceItem> results = new ArrayList<>();
    XmlPullParser xpp = context.getResources().getXml(item.getResId());

    try {
        xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        xpp.setFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, true);
        ArrayList<String> breadcrumbs = new ArrayList<>();
        ArrayList<String> keyBreadcrumbs = new ArrayList<>();
        if (!TextUtils.isEmpty(item.getBreadcrumb())) {
            breadcrumbs.add(item.getBreadcrumb());
        }
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                PreferenceItem result = parseSearchResult(xpp);
                result.resId = item.getResId();

                if (!BLACKLIST.contains(xpp.getName()) && result.hasData()) {
                    result.breadcrumbs = joinBreadcrumbs(breadcrumbs);
                    result.keyBreadcrumbs = cleanupKeyBreadcrumbs(keyBreadcrumbs);
                    if (!"true".equals(getAttribute(xpp, NS_SEARCH, "ignore"))) {
                        results.add(result);
                    }
                }
                if (CONTAINERS.contains(xpp.getName())) {
                    breadcrumbs.add(result.title == null ? "" : result.title);
                }
                if (xpp.getName().equals("PreferenceScreen")) {
                    keyBreadcrumbs.add(getAttribute(xpp, "key"));
                }
            } else if (xpp.getEventType() == XmlPullParser.END_TAG && CONTAINERS.contains(xpp.getName())) {
                breadcrumbs.remove(breadcrumbs.size() - 1);
                if (xpp.getName().equals("PreferenceScreen")) {
                    keyBreadcrumbs.remove(keyBreadcrumbs.size() - 1);
                }
            }

            xpp.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return results;
}