编程语言
首页 > 编程语言> > java – 在Android中使用Simple XML解析XML文件列表元素

java – 在Android中使用Simple XML解析XML文件列表元素

作者:互联网

我需要用SImple XML解析一个大的xml文件,(我真的想使用Simple XML).我使用XSD创建了对象,将它们从JAXB特定转换为特定于SimpleXML的对象.

XML看起来像这样:

    <House>
      <MainLevel Name="~#editRoom" IsHidden="false">
        <ChildLevel Name="Television" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="Chair" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="Table">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="ChamberName" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
          <ChildLevel Name="ChamberName" Category="Bathroom">
          <string>BathTub</string>
        </ChildLevel>
         <ChildLevel Name="Door", Category="DiningRoom">
          <boolean>isOpen</boolean>
        </ChildLevel>
     </MainLevel>
    <MainLevel Name="~#editRoom" IsHidden="false">
        <ChildLevel Name="Television" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="Chair" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="Table" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
         <ChildLevel Name="ChamberName" Category="Livingroom">
          <string>TestRoom</string>
        </ChildLevel>
          <ChildLevel Name="ChamberName" Category="Bathroom">
          <string>BathTub</string>
        </ChildLevel>
         <ChildLevel Name="Door">
          <boolean>isOpen</boolean>
        </ChildLevel>
     </MainLevel>
</House>

你有什么建议.请帮忙.Thx.

解决方法:

你最好写3个班:

> House类,(=根)包含MainLevel的(内联)列表
> MainLevel类,包含所有ChildLevel的(内联)列表
> ChildLevel类,包含值

这是一些伪代码:

@Root(...)
public class House
{
    @ElementList(inline = true, ...)
    private List<MainLevel> levels;

    // ...
}

public class MainLevel
{
    @Attribute(name = "Name")
    private String name;
    @Attribute(name = "IsHidden")
    private bool hidden;
    @ElementList(inline = true, ...)
    private List<ChildLevel> childLevels;

    // ...
}

public class ChildLevel
{
    @Attribute(name = "Name")
    private String name;
    @Attribute(name = "Category", required = false)
    private String category;

    // ...
}

由于ChildLevel可以有不同的类型,因此您必须注意这一点.要么实现所有类型并将它们标记为不需要,要么创建子类.

标签:java,android,xml,jaxb,simple-framework
来源: https://codeday.me/bug/20190629/1322975.html