5

当我需要访问已编译的 Android XML 资源时,我似乎无法使用openRawResource(resId),因为它为我提供了已编译的二进制文件。

我发现获取 XML 的唯一方法是使用getXml(resId)返回XmlResourceParser(实现XmlPullParser)。

如何方便地从 Android 资源中反序列化复杂的编译 XML(但不大)?

我试图破解Simple XML 框架:

val serializer = new Persister()
serializer.read(classOf[AFoo], 
    new NodeReader(new PullReader(r.getXml(resId))).readRoot)

(上面的 Scala 代码被简化了。)

但是它失败了:

java.lang.RuntimeException: getPrefix not supported
    at android.content.res.XmlBlock$Parser.getPrefix(XmlBlock.java:156)
    at org.simpleframework.xml.stream.PullReader$Start.<init>(PullReader.java:373)
    at org.simpleframework.xml.stream.PullReader.start(PullReader.java:143)
    at org.simpleframework.xml.stream.PullReader.read(PullReader.java:109)
    at org.simpleframework.xml.stream.PullReader.read(PullReader.java:117)
    at org.simpleframework.xml.stream.PullReader.next(PullReader.java:89)
    at org.simpleframework.xml.stream.NodeReader.readElement(NodeReader.java:111)
    at org.simpleframework.xml.stream.NodeReader.readRoot(NodeReader.java:85)
  • 是否有任何不调用的 XML 反序列化器getPrefix
  • 或者是否已经存在从 Scala 库构建Node树的XmlPullParser?(因此可以使用 Scala 投影函数对其进行查询。)
  • 或者您有任何其他提示如何从资源中读取小而复杂的编译 XML?
4

2 回答 2

0

似乎您使用的 XmlPull 版本不支持命名空间。尝试使用

org.simpleframework.xml.stream.DocumentProvider

这使用 w3c DOM。

于 2013-04-12T10:35:50.760 回答
0

我做了一个包装类,终于解决了这个问题。(我使用的是 Java 而不是 Scala。)

Persister serializer = new Persister();
serializer.read(AFoo.class, 
    new NodeReader(new PullReader(new XmlPullParserWrapper(r.getXml(resId)))).readRoot())

在哪里

public class XmlPullParserWrapper implements XmlPullParser {
    private XmlPullParser mParser;

    public XmlPullParserWrapper(XmlPullParser parser) {
        mParser = parser;
    }

    @Override
    public void setFeature(String name, boolean state) throws XmlPullParserException {
        mParser.setFeature(name, state);
    }

    @Override
    public boolean getFeature(String name) {
        return mParser.getFeature(name);
    }

    @Override
    public void setProperty(String name, Object value) throws XmlPullParserException {
        mParser.setProperty(name, value);
    }

    @Override
    public Object getProperty(String name) {
        return mParser.getProperty(name);
    }

    @Override
    public void setInput(Reader in) throws XmlPullParserException {
        mParser.setInput(in);
    }

    @Override
    public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException {
        mParser.setInput(inputStream, inputEncoding);
    }

    @Override
    public String getInputEncoding() {
        return mParser.getInputEncoding();
    }

    @Override
    public void defineEntityReplacementText(String entityName, String replacementText) throws XmlPullParserException {
        mParser.defineEntityReplacementText(entityName, replacementText);
    }

    @Override
    public int getNamespaceCount(int depth) throws XmlPullParserException {
        return mParser.getNamespaceCount(depth);
    }

    @Override
    public String getNamespacePrefix(int pos) throws XmlPullParserException {
        return mParser.getNamespacePrefix(pos);
    }

    @Override
    public String getNamespaceUri(int pos) throws XmlPullParserException {
        return mParser.getNamespaceUri(pos);
    }

    @Override
    public String getNamespace(String prefix) {
        return mParser.getNamespace(prefix);
    }

    @Override
    public int getDepth() {
        return mParser.getDepth();
    }

    @Override
    public String getPositionDescription() {
        return mParser.getPositionDescription();
    }

    @Override
    public int getLineNumber() {
        return mParser.getLineNumber();
    }

    @Override
    public int getColumnNumber() {
        return mParser.getColumnNumber();
    }

    @Override
    public boolean isWhitespace() throws XmlPullParserException {
        return mParser.isWhitespace();
    }

    @Override
    public String getText() {
        return mParser.getText();
    }

    @Override
    public char[] getTextCharacters(int[] holderForStartAndLength) {
        return mParser.getTextCharacters(holderForStartAndLength);
    }

    @Override
    public String getNamespace() {
        return mParser.getNamespace();
    }

    @Override
    public String getName() {
        return mParser.getName();
    }

    @Override
    public String getPrefix() {
        return "";
    }

    @Override
    public boolean isEmptyElementTag() throws XmlPullParserException {
        return mParser.isEmptyElementTag();
    }

    @Override
    public int getAttributeCount() {
        return mParser.getAttributeCount();
    }

    @Override
    public String getAttributeNamespace(int index) {
        return mParser.getAttributeNamespace(index);
    }

    @Override
    public String getAttributeName(int index) {
        return mParser.getAttributeName(index);
    }

    @Override
    public String getAttributePrefix(int index) {
        return "";
    }

    @Override
    public String getAttributeType(int index) {
        return mParser.getAttributeType(index);
    }

    @Override
    public boolean isAttributeDefault(int index) {
        return mParser.isAttributeDefault(index);
    }

    @Override
    public String getAttributeValue(int index) {
        return mParser.getAttributeValue(index);
    }

    @Override
    public String getAttributeValue(String namespace, String name) {
        return mParser.getAttributeValue(namespace, name);
    }

    @Override
    public int getEventType() throws XmlPullParserException {
        return mParser.getEventType();
    }

    @Override
    public int next() throws XmlPullParserException, IOException {
        return mParser.next();
    }

    @Override
    public int nextToken() throws XmlPullParserException, IOException {
        return mParser.nextToken();
    }

    @Override
    public void require(int type, String namespace, String name) throws XmlPullParserException, IOException {
        mParser.require(type, namespace, name);
    }

    @Override
    public String nextText() throws XmlPullParserException, IOException {
        return mParser.nextText();
    }

    @Override
    public int nextTag() throws XmlPullParserException, IOException {
        return mParser.nextTag();
    }
}
于 2015-07-06T15:45:45.670 回答