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文件中的属性。
<?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文件中的属性。
相关文章推荐
- Java集合(三):Queue队列
- OpenStack安装时Compute节点连不上RabbitMQ
- link和@import两种导入css文件的区别。
- phpstudy APACHE支持.htaccess以及 No input file specified解决方案
- HDFS源码分析之数据块Block、副本Replica
- 为何使用消息系统
- 第六周 用多文件组织多个类的程序(带武器的角色类)
- c++通过ADO方式操作SQLSERVER数据库
- 申论备考攻略:议论文中两大论证方法
- ASP.NET MVC ApiController过滤器预处理表单数据
- Android APK反编译就这么简单 详解(附图)
- Hadoop、Spark、HBase与Redis的适用性讨论
- 数据结构图的运算(深度优先)
- c++作业3
- python 标准库获取网络信息
- Android APP终极瘦身指南
- Unity3d-tolua基础一
- 通过复杂的代码形成业务数据并显示的思路 游标 存储过程
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
- Qt5 VS2013中文乱码解决