XmlPullParserException 不支持的特性。

3

我正在尝试从我的Android项目中的jar文件中读取一个drawable xml文件。

private Drawable readDrawableFromJar(String p_strFilePath) {
        InputStream stream = getClass().getClassLoader().getResourceAsStream(p_strFilePath);
        XmlPullParser parser = null;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(stream, "UTF-8");
        } catch (XmlPullParserException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        }


        Drawable drawable = null;
        try {
            if (parser != null) {
                drawable = VectorDrawableCompat.createFromXml(getContext().getResources(), parser);

               return drawable ;
            } else {
                Log.e(LOG_TAG, "parser is null for " + p_strFilePath);
            }

        } catch (XmlPullParserException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        }
       return null;
    }

这个方法可以找到文件并读取文本内容。但是,当它设置输入时,会抛出以下异常:

org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#validation (position:START_DOCUMENT null@1:1)

这真的很奇怪,因为XML文件中不包含任何这样的文本:http://xmlpull.org/v1/doc/features.html#validation

这是位于JAR文件中的XML文件。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:width="24dp"
    android:height="24dp">
    <path
        android:pathData="M12 3C7.031 3 3 7.027 3 12.001 3 16.967 7.031 21 12 21 16.971 21 20.999 16.967 20.999 12.001 20.998 7.027 16.971 3 12 3m0 20C5.925 23 1 18.073 1 12.001 1 5.925 5.925 1 12 1 18.077 1 23 5.925 23 12.001 22.998 18.073 18.076 23 12 23"
        android:fillColor="#242838" />
    <path
        android:pathData="M13 18h-2v-7h2z"
        android:fillColor="#242838" />
    <path
        android:pathData="M10 10h3v2h-3z"
        android:fillColor="#242838" />
    <path
        android:pathData="M10 16h4v2h-4z"
        android:fillColor="#242838" />
    <path
        android:pathData="M11 6h2v2h-2z"
        android:fillColor="#242838" />
</vector>
1个回答

5
你面临的问题与你正在验证的xml文件的内容无关。相反,XmlPullParserFactory.setValidating(true) 行似乎是你得到异常的原因。这行代码的作用是配置创建的解析器来验证xml。不幸的是,由XmlPullParserFactory.newPullParser() 创建的解析器实现似乎不支持验证。在你的代码中注释掉 factory.setValidating(true) 这一行应该可以解决问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接