xml文件中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<Tasks>
<Group name="1" Type="true">
<task ID="1" Name="F1" Type="true" Priority="1"> </task>
<task ID="2" Name="F2" Type="true" Priority="2"> </task>
<task ID="3" Name="F3" Type="true" Priority="3"> </task>
<task ID="4" Name="F4" Type="false" Priority="1"> </task>
<task ID="5" Name="F5" Type="true" Priority="1"> </task>
</Group>
<Group name="2" Type="false">
<task ID="1" Name="F6" Type="true" Priority="1"> </task>
<task ID="2" Name="F7" Type="true" Priority="2"> </task>
<task ID="3" Name="F8" Type="true" Priority="3"> </task>
<task ID="4" Name="F9" Type="false" Priority="1"> </task>
<task ID="5" Name="F10" Type="true" Priority="1"> </task>
</Group>
</Tasks>
解析方式:
方式1:如果要得到Name和Type两个属性:
Map<String,String> tasks = new HashMap<String,String>();
tasks.put(Name,Type);
方式2:如果要得到Group,Name,Type三个属性:
Map<String,Map<String,String>> tasks = new HashMap<String,Map<String,String>>(); //嵌套的HashMap
Map<String,String> task = new HashMap<String,String>();
tasks.put(Name,Type);
tasks.put(Group,task);
方式3:如果要得到Group,Name,Type,Priority4个属性:
Map<String,String> tasks = new HashMap<String,String>();
task.put("Group1","Name=F15,Type=true,Priority=1");
方式4:如果要得到Group,Name,Type,Priority4个属性:
定义一个class类,将这4个属性定义到class类中的4个变量,通过操作class类中的属性获得xml文件中的属性。
<?xml version="1.0" encoding="UTF-8"?>
<Tasks>
<Group name="1" Type="true">
<task ID="1" Name="F1" Type="true" Priority="1"> </task>
<task ID="2" Name="F2" Type="true" Priority="2"> </task>
<task ID="3" Name="F3" Type="true" Priority="3"> </task>
<task ID="4" Name="F4" Type="false" Priority="1"> </task>
<task ID="5" Name="F5" Type="true" Priority="1"> </task>
</Group>
<Group name="2" Type="false">
<task ID="1" Name="F6" Type="true" Priority="1"> </task>
<task ID="2" Name="F7" Type="true" Priority="2"> </task>
<task ID="3" Name="F8" Type="true" Priority="3"> </task>
<task ID="4" Name="F9" Type="false" Priority="1"> </task>
<task ID="5" Name="F10" Type="true" Priority="1"> </task>
</Group>
</Tasks>
解析方式:
方式1:如果要得到Name和Type两个属性:
Map<String,String> tasks = new HashMap<String,String>();
tasks.put(Name,Type);
方式2:如果要得到Group,Name,Type三个属性:
Map<String,Map<String,String>> tasks = new HashMap<String,Map<String,String>>(); //嵌套的HashMap
Map<String,String> task = new HashMap<String,String>();
tasks.put(Name,Type);
tasks.put(Group,task);
方式3:如果要得到Group,Name,Type,Priority4个属性:
Map<String,String> tasks = new HashMap<String,String>();
task.put("Group1","Name=F15,Type=true,Priority=1");
方式4:如果要得到Group,Name,Type,Priority4个属性:
定义一个class类,将这4个属性定义到class类中的4个变量,通过操作class类中的属性获得xml文件中的属性。