有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java简单XML框架解析器在Android中发布XML解析

我在Android应用程序中解析XML时遇到问题,一个问题here意味着建议使用4种类型的XML解析机制: -萨克斯 -多姆 -XmlPullParser -简单XML框架

虽然Simple Framework很神奇,我已经在使用它了,但当我发现它不能在同一个类中支持@Text和@Element时,我却陷入了死胡同,这很糟糕,因为我无法更改我的XML方案

因此,我们欢迎您的想法,任何建议都将是非常好的


共 (3) 个答案

  1. # 2 楼答案

    经过长时间的研究,我发现最适合我需要的XML解析器是JAXB解析器

    • 支持复杂类型
    • 面向注释
    • 与Android配合良好
    • 很容易理解
    • Java 1.6+中已经存在生成JAXB注释类的工具

    下面是一个示例,展示了它的易用性:

    @XmlRootElement(name = "a")
     public class A {
    
    @XmlElementRefs({
        @XmlElementRef(name = "lang", namespace = "http://www.w3.org/namespace/", type = Lang.class, required = false),
        @XmlElementRef(name = "subst", namespace = "http://www.w3.org/namespace/", type = Subst.class, required = false),
        @XmlElementRef(name = "include", namespace = "http://www.w3.org/namespace/", type = Include.class, required = false),
        @XmlElementRef(name = "br", namespace = "http://www.w3.org/namespace/", type = Br.class, required = false),
        @XmlElementRef(name = "kw", namespace = "http://www.w3.org/namespace/", type = Kw.class, required = false),
        @XmlElementRef(name = "help", namespace = "http://www.w3.org/namespace/", type = Help.class, required = false)
    })
    @XmlMixed
    protected List<Object> content;
    @XmlAttribute(name = "cost")
    protected String cost;
    @XmlAttribute(name = "href", required = true)
    protected String href;
    @XmlAttribute(name = "key")
    protected String key;
    

    所以这是我想到的最好的


    欢迎添加任何内容:)

  2. # 3 楼答案

    您可以使用Konsume-XML:它基于Stax/pull,但级别更高,更易于使用。默认情况下,它不会将内容映射到对象,但可以很容易地使用它。请参见KonsumeXML页面上的更多示例