使用简单XML解析XML [Java Android]

时间:2016-05-26 09:19:17

标签: java android xml simple-framework

如何解析此XML:

<resources> 
    <string name="name1">content1</string> 
    <string name="name2">content2</string> 
    <string name="name3">content3</string> 
    ...
</resources> 

我无法使用正确的注释创建对象来检索名称和内容。

我目前的工作:

@Root(name = "resources")
public class Translation {

    @ElementList(inline = true)
    private List<TranslationName> translationNames;

    public List<TranslationName> getTranslationNames() {
        return translationNames;
    }
}

@Root(name = "string")
public class TranslationName {

    @Attribute(name = "name")
    private String name;
    @Element(name = "string")
    private String content;

    public String getName() {
        return name;
    }

    public String getContent() {
        return content;
    }
}

但我有:

  

无法满足@ org.simpleframework.xml.Element(data = false,   字段&#39;内容&#39;

上的name = string,required = true,type = void)

修改

有了这个我成功恢复了内容:

@Root(name = "resources")
public class Translation {

    @ElementList(inline = true)
    private List<String> contentNames;

    public List<String> getContentNames() {
        return contentNames;
    }
}

但是两者结合起来并不起作用:

@Root(name = "resources")
public class Translation {

    @ElementList(inline = true)
    private List<TranslationName> translationNames;
    @ElementList(inline = true)
    private List<String> contentNames;

    public List<TranslationName> getTranslationNames() {
        return translationNames;
    }

    public List<String> getContentNames() {
        return contentNames;
    }
}

2 个答案:

答案 0 :(得分:0)

你的类给出了像

这样的xml
 <resources>
   <string name="name1"> <string>content1</string>   </string>
   <string name="name2"> <string>content2</string>   </string>
   <string name="name3"><string>content3</string>   </string>
 </resources>

用于内容属性替换为:

@Text
    private String content;

答案 1 :(得分:0)

您可以尝试自己编写XML解析器,而不必尝试使用批注的魔术组合来使事情发生。不要使用Stax / pull-这太底层了,很难直接使用,而且容易出错。尝试Konsume-XML

       db_id           cert_status grp_id       year   cap prov
133   IX-011  not-certified member     SD 2007-01-01  30.0   KB
134   IX-011  not-certified member     SD 2008-01-01  30.0   KB
135   IX-011  not-certified member     SD 2009-01-01  30.0   KB
136   IX-011  not-certified member     SD 2010-01-01  30.0   KB
137   IX-011  not-certified member     SD 2011-01-01  30.0   KB
138   IX-011  not-certified member     SD 2012-01-01  30.0   KB
139   IX-011  not-certified member     SD 2013-01-01  30.0   KB
140   IX-011  not-certified member     SD 2014-01-01  30.0   KB
141   IX-011  not-certified member     SD 2015-01-01  30.0   KB
142   IX-011  not-certified member     SD 2016-01-01  30.0   KB
215   IX-017  not-certified member     CG 2011-01-01  30.0   KB
216   IX-017  not-certified member     CG 2012-01-01  30.0   KB
217   IX-017  not-certified member     CG 2013-01-01  80.0   KB
218   IX-017  not-certified member     CG 2014-01-01  30.0   KB
219   IX-017  not-certified member     CG 2015-01-01  30.0   KB
220   IX-017  not-certified member     CG 2016-01-01  30.0   KB

将打印

data class Resource(val name: String, val content: String)

val konsumer = """
<resources>
    <string name="name1">content1</string>
    <string name="name2">content2</string>
    <string name="name3">content3</string>
</resources>
""".trimIndent().konsumeXml()
val resources: List<Resource> = konsumer.child("resources") {
    children("string") {
        Resource(attributes["name"], text())
    }
}
println(resources)