xml文件解析的几种方式

本文介绍了一种从XML文件中解析任务数据的方法,并提出了四种不同的数据存储方案。这些方案涵盖了使用不同层级的HashMap来组织数据,以及创建专门的类来封装属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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文件中的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值