Converting XML text to many different classes



My issue is converting text within a field to a specific class (for purposes of launching different Activities). My XML file will be expanded into two different classes. However, each object will have a reference to another class (which is an Activity), and this reference is unique for each item in the XML file. So, it will look similar to the following, albeit with more properties:



<Group name="Header 1">
<DestinationClass>ActivityA.class</DestinationClass>
<Child name="Child 1">
<DestinationClass>ActivityA1.class</DestinationClass>
</Child>
<Child name="Child 2">
<DestinationClass>ActivityA2.class</DestinationClass>
</Child>
</Group>

<Group name="Header 2">
<DestinationClass>ActivityB.class</DestinationClass>
<Child name="Child 1">
<DestinationClass>ActivityB1.class</DestinationClass>
</Child>
</Group>


In the end, I'm trying to mimic AndroidManifest.xml file in a way. This XML file will be parsed then eventually turned into an ExpandableListView on my Navigation Drawer. Ultimately, I'm trying to avoid coding in every detail of the menu, and would rather also have it all in one place. The classes look similar to the following:



public class NavListGroup {
// Id for this group, for later matching
private int mGroupId;

// The displayed name of this Group
private String mGroupName;

// As needed for now, only launches an activity
// Determines the destination
private Class mActivityDestination;

// Easily determines if the group has children under it
private boolean mHasChildren = false;

// Holds all the children items that belongs under this group
private List<NavListChild> mChildrenList;

}


public class NavListChild {
// Id for this child item, for later matching
private int mChildId;

// The displayed name of this child item
private String mChildName;

// As needed for now, only launches an activity
// Determines the destination
private Class mActivityDestination;
}


(The XML also holds the ID as well, if I still need it in the end)


The reason for needing the member Class variable is that, once the group or child item is clicked, an intent will be launched- however, every item has a different activity/class. I could use a master list [matched via Ids to classes in a second list that's coded directly in], but that would go against the purpose of making the XML file in the first place.


I've tried searching everywhere for an answer- it's not really serializing objects (although I'm guessing it's considered deserializing XML into two objects, in this case), but it's more of a problem of converting a text to a class similar to in the AndroidManifest xml file, where an Activity is defined. I also couldn't find the code that parses the AndroidManifest file either, so no leads there.


No comments:

Post a Comment