简体   繁体   中英

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). I created objects with XSD, converted them from JAXB specific to SimpleXML specific adnotated objects.

The XML looks like this:

    <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.

You best write 3 classes:

  1. A House class , (= the root) containing a (inline-) list of MainLevel
  2. A MainLevel class , containing a (inline-) list of all ChildLevel
  3. A ChildLevel class , containing the value

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. Either implement all types and mark them as not required, or make subclasses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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