您的位置:首页 > 其它

xml文件解析的几种方式

2016-04-13 17:03 344 查看
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文件中的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: