方便使用Android XmlResourceParser?

时间:2013-04-11 08:44:25

标签: android scala xml-parsing android-resources simple-framework

当我需要访问已编译的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
  • 或者是否已存在可从Node构建XmlPullParser树的Scala库? (因此可以使用Scala投影函数进行查询。)
  • 或者您是否有其他提示如何从资源中读取小而复杂的编译XML?

2 个答案:

答案 0 :(得分:0)

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

org.simpleframework.xml.stream.DocumentProvider

这使用w3c DOM。

答案 1 :(得分: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();
    }
}