使用dom4j解析xml字符串

一.解析xml字符串

1.要解析的字符串:

<?xml version="1.0" encoding="UTF-8"?>
<root label="功能菜单">
  <menu id="11" label="工作报告" url="" type="1">
    <menu id="12" label="待办事项" url="com/xdja/weeklyReport/agenda/ShowAgenda.swf" type="1" />
    <menu id="13" label="工作报告" url="com/xdja/weeklyReport/personalReport/ShowReport.swf" type="1" />
    <menu id="51" label="周报查看" url="com/xdja/weeklyReport/personalReport/Query.swf" type="1" />
    <menu id="56" label="周报审批" url="com/xdja/weeklyReport/personalReport/Approve.swf" type="1" />
  </menu>
  <menu id="67" label="知识库" url="com/xdja/knowledge/FileMain.swf" type="1" />
  <menu id="14" label="绩效管理" url="" type="1">
    <menu id="15" label="个人绩效查看" url="com/xdja/performance/PersonnelPerfView.swf" type="1" />
    <menu id="46" label="部门绩效查看" url="com/xdja/performance/DeptPerfView.swf" type="1" />
    <menu id="47" label="部门绩效管理" url="com/xdja/performance/DeptPerfManager.swf" type="1" />
    <menu id="57" label="当月工作量查看" url="com/xdja/performance/PerWorkloadView.swf" type="1" />
    <menu id="58" label="员工工作量查看" url="com/xdja/performance/DeptPerwView.swf" type="1" />
    <menu id="62" label="任务补发率查看" url="com/xdja/performance/ReplacementView.swf" type="1" />
    <menu id="59" label="部门绩效打印管理" url="com/xdja/performance/DeptPerfPrintManager.swf" type="1" />
    <menu id="65" label="工作量报表统计" url="com/xdja/report/WorkloadReport.swf" type="1" />
  </menu>
  <menu id="16" label="绩效相关配置" url="" type="1">
    <menu id="17" label="月满额工作量配置" url="com/xdja/performanceRelatedConfig/MonthWorkload.swf" type="1" />
  </menu>
  <menu id="18" label="质量保证金管理" url="" type="1">
    <menu id="22" label="个人账户查看" url="com/xdja/qg/PersonnelAccountManage.swf" type="1" />
    <menu id="20" label="质保金缺陷查询" url="com/xdja/qg/DefectQuery.swf" type="1" />
    <menu id="21" label="质保金账户管理" url="com/xdja/qg/QgAccountManage.swf" type="1" />
    <menu id="53" label="质保金缺陷管理" url="com/xdja/qg/DefectManage.swf" type="1" />
    <menu id="50" label="质保金缺陷添加" url="com/xdja/qg/AddQgDefect.swf" type="1" />
  </menu>
  <menu id="23" label="项目管理" url="" type="1">
    <menu id="24" label="项目查询" url="com/xdja/projectManage/Query.swf" type="1" />
    <menu id="25" label="项目信息配置" url="com/xdja/project/projectInfo/ProjectInfo.swf" type="1" />
    <menu id="26" label="项目任务与进度" url="com/xdja/projectManage/Tasks.swf" type="1" />
    <menu id="27" label="项目周报" url="com/xdja/project/weekly/showWeekly.swf" type="1" />
    <menu id="28" label="项目月报" url="com/xdja/project/monthly/showMonthly.swf" type="1" />
    <menu id="29" label="项目缺陷报告" url="com/xdja/project/defectReport/DefectReport.swf" type="1" />
    <menu id="30" label="项目综合绩效考核" url="com/xdja/project/performance/Performance.swf" type="1" />
    <menu id="31" label="项目成员工作量跟踪" url="com/xdja/projectManage/PersonalWorkload.swf" type="1" />
  </menu>
  <menu id="37" label="简易任务管理" url="" type="1">
    <menu id="38" label="简易任务查询与录入" url="com/xdja/simpleTask/showSimpleTask.swf" type="1" />
  </menu>
  <menu id="1" label="基础信息管理" url="" type="1">
    <menu id="2" label="部门管理" url="com/xdja/deptManage/showDept.swf" type="1" />
    <menu id="3" label="人员管理" url="com/xdja/empManage/showEmp.swf" type="1" />
    <menu id="4" label="角色管理" url="com/xdja/roleManage/showRole.swf" type="1" />
    <menu id="5" label="字典管理" url="com/xdja/dictManage/showDict.swf" type="1" />
    <menu id="39" label="系统配置" url="com/xdja/system/sysConfigInfo.swf" type="1" />
  </menu>
  <menu id="63" label="附件程序" url="" type="1">
    <menu id="64" label="通讯录" url="com/xdja/program/Contacts.swf" type="1" />
  </menu>
  <menu id="69" label="新闻" url="" type="1">
    <menu id="70" label="新闻查询" url="com/xdja/news/NewsQuery.swf" type="1" />
    <menu id="71" label="新建新闻" url="com/xdja/news/CreatNews.swf" type="1" />
    <menu id="72" label="新闻管理" url="com/xdja/news/NewsManager.swf" type="1" />
  </menu>
  <menu id="6" label="变更密码" url="com/xdja/system/alterPwd.swf" type="1" />
  <menu id="7" label="安全退出" url="exit" type="1" />
  <menu id="75" label="通知公告" url="" type="1">
    <menu id="76" label="通告查询" url="com/xdja/notices/NoticesQuery.swf" type="1" />
    <menu id="77" label="新建通告" url="com/xdja/notices/CreatNotice.swf" type="1" />
    <menu id="78" label="通告管理" url="com/xdja/notices/NoticesManager.swf" type="1" />
    <menu id="79" label="通告审批" url="com/xdja/notices/Approve.swf" type="1" />
  </menu>
</root>

 

2.解析方法:

public String xmlData(String xmlData){
		try {
			Document document = DocumentHelper.parseText(userBean.getMenuXml());
			Element root = document.getRootElement();//获取根节点
			Iterator it = root.elements().iterator();//获得根节点下的子节点的集合
			while (it.hasNext()) {//遍历集合
				Element info = (Element) it.next();//获取根节点下的子节点
				System.out.println(info.attributeValue("label"));//获取子节点下的label属性值
				Iterator infoIt = info.elements().iterator();
				while (infoIt.hasNext()) {
					Element infoChild = (Element) infoIt.next();
					System.out.println("   " + infoChild.attributeValue("label"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return null;
	}

 

3.运行输出结果:

工作报告
   待办事项
   工作报告
   周报查看
   周报审批
知识库
绩效管理
   个人绩效查看
   部门绩效查看
   部门绩效管理
   当月工作量查看
   员工工作量查看
   任务补发率查看
   部门绩效打印管理
   工作量报表统计
绩效相关配置
   月满额工作量配置
质量保证金管理
   个人账户查看
   质保金缺陷查询
   质保金账户管理
   质保金缺陷管理
   质保金缺陷添加
项目管理
   项目查询
   项目信息配置
   项目任务与进度
   项目周报
   项目月报
   项目缺陷报告
   项目综合绩效考核
   项目成员工作量跟踪
简易任务管理
   简易任务查询与录入
基础信息管理
   部门管理
   人员管理
   角色管理
   字典管理
   系统配置
附件程序
   通讯录
新闻
   新闻查询
   新建新闻
   新闻管理
变更密码
安全退出
通知公告
   通告查询
   新建通告
   通告管理
   通告审批

二.解析xml文件 

  1.xml文件内容

 

<?xml version="1.0" encoding="UTF-8"?>
<rrot>
	<model name="通告查询">/new/tonggao.action</model>
	<model name="新闻查询">/new/new.action</model>
	<model name="工作流">/new/workflow.action</model>
</rrot>

 

2.解析代码

public String getModelUrl(){
		try {
			SAXReader reader = new SAXReader();
			URI url=BaseAction.class.getResource("modelurl.xml").toURI();//获取xml路径
			Document document=reader.read(new File(url));//加载xml
			Element rootNode=document.getRootElement();//获取根路径
			Iterator rootNodeIt=rootNode.elements().iterator();//获取根路径下子节点的集合
			//遍历集合
			while(rootNodeIt.hasNext()){
				Element infoNode=(Element)rootNodeIt.next();//获取单个子节点
				System.out.println(infoNode.attributeValue("name")+":"+infoNode.getText());
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return null;
	}

 

3.测试输出数据

通告查询:/new/tonggao.action
新闻查询:/new/new.action
工作流:/new/workflow.action

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值