如何解析此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、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;
}
}发布于 2016-05-26 12:21:17
类给出的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;发布于 2019-04-12 17:29:54
您可以自己编写XML解析器,而不是尝试使用神奇的注释组合来实现某些事情。不要使用Stax/pull -这是太低,硬和错误容易直接使用。试试Konsume
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)将打印
[Resource(name=name1, content=content1), Resource(name=name2, content=content2), Resource(name=name3, content=content3)]https://stackoverflow.com/questions/37456580
复制相似问题