简体   繁体   English

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

[英]Parsing XML file list elements with Simple XML in Android

I need to parse a large xml file with SImple XML, (I really want to use Simple XML). 我需要用SImple XML解析一个大的xml文件,(我真的想使用Simple XML)。 I created objects with XSD, converted them from JAXB specific to SimpleXML specific adnotated objects. 我使用XSD创建了对象,将它们从JAXB特定转换为特定于SimpleXML的对象。

The XML looks like this: 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>

What are your suggestions. 你有什么建议。 Please help.Thx. 请帮忙.Thx。

You best write 3 classes: 你最好写3个班:

  1. A House class , (= the root) containing a (inline-) list of MainLevel House ,(=根)包含MainLevel的(内联)列表
  2. A MainLevel class , containing a (inline-) list of all ChildLevel MainLevel ,包含所有ChildLevel的(内联)列表
  3. A ChildLevel class , containing the value ChildLevel ,包含值

Here's some pseudocode : 这是一些伪代码

@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;

    // ...
}

Since a ChildLevel can have different types, you have to take care about this. 由于ChildLevel可以有不同的类型,因此您必须注意这一点。 Either implement all types and mark them as not required, or make subclasses. 要么实现所有类型并将它们标记为不需要,要么创建子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM