使用dom4j解析xml文档
来源:互联网 发布:linux查看磁盘空间 df 编辑:程序博客网 时间:2024/06/08 12:11
一. DOM4J概述
DOM4J是一个易用的、开源的库,可以实现对XML文档的解析。DOM4J使用起来非常简单,只要了解基本的XML-DOM模型,就能使用。DOM4J最大的特色是使用大量的接口。目前,DOM4J的使用越来越广泛。
二. 使用DOM4J操作XML数据
要使用DOM4J读写XML文档,需要先下载DOM4J包,在DOM4J官方网站下载后将相应的包加人工程就可以使用了。
一些关键api介绍
1.Document对象相关
读取XML文件,获得Document对象。
SAXReader reader = new SAXReader();
Documet document = reader.read(new File("input.xml"));
2.节点相关
(1)获取文档的根元素。
Element root = document.getRootElement();
(2)取得某节点的单个子节点。
Element memberElm = root.element(“member”);
(3)取得节点的文字。
String text = memberElm.getText();
(4)取得某节点下名为”member”的所有字节点并进行遍历。
List nodeList = rootElm.elements(“member”);
(5)在某节点下添加子节点。
ElementageElm = newMemberElm.addElement(“age”);
(6)设置节点文字。
ageElm.setText(“29”);
(7)删除某节点。
parentElm.remove(childElm);//childElm是特删除的节点,parentElm是其父节点
3.属性相关
(1)取得某节点下的某属性。
Element root = document.getRootElement();
Attribute attribute = root.attribute(“size”);//属性名为name
(2)取得属性的文字。
String text = attribute.getText();
也可以一步获取属性值:
String attrValue=oneElement.attributeValue("name");
(3)设置某节点的属性和文字。
newMemeberElm.addAttribute(“name”,”sitinspring”);
(4)设置属性的文字。
Attribute attribute = root.attribute(“name”);
attribute.setText(“sitinspring”);
根据上面总结的关键操作代码,可以实现使用DOM4J对XML文件数据的查询、添加、修改和删除功能。
下面通过示例2学习如何使用DOM4J读取XML的数据。
================mystruts.xml=========================
<!DOCTYPE mystruts[<!ELEMENT mystruts (package)><!ELEMENT package (action*)><!ELEMENT action (result*)><!ELEMENT result (#PCDATA)><!ATTLIST action name CDATA #REQUIRED class CDATA #REQUIRED><!ATTLIST result name CDATA #IMPLIED redirect (true|false) "false"> ]><mystruts><package><action name="Login" class="com.obtk.actions.LoginAction"><result name="success">index.jsp</result><result name="input" redirect="true">Login.jsp</result></action><action name="Register" class="com.obtk.actions.RegisterAction"><result name="success">Login.jsp</result><result name="input" redirect="true">Register.jsp</result></action></package></mystruts>
package com.obtk.xml;import java.io.File;import java.util.HashMap;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import com.obtk.entity.ActionMapping;import com.obtk.entity.Result;public class XmlParser {public static void main(String[] args) {//获得工程路径String path=System.getProperty("user.dir");File configFile=new File(path+"\\src\\mystruts.xml");SAXReader reader=new SAXReader();Map<String,ActionMapping> actionMap=new HashMap<String, ActionMapping>();try {//1.获得文档对象Document doc=reader.read(configFile);//2.获得根元素Element root=doc.getRootElement();Element packageEle=root.element("package");List<Element> actionEleList=packageEle.elements("action");Element actionEle=null;ActionMapping oneAction=null;for(int i=0;i<actionEleList.size();i++){oneAction=new ActionMapping();actionEle=actionEleList.get(i);System.out.println("名称:"+actionEle.attributeValue("name"));System.out.println("类名:"+actionEle.attributeValue("class"));oneAction.setActionName(actionEle.attributeValue("name"));oneAction.setClassName(actionEle.attributeValue("class"));System.out.println("=================");List<Element> resultEleList=actionEle.elements("result");Map<String,Result> resultMap=new HashMap<String, Result>();Result oneReuslt=null;Element resultEle=null;for(int j=0;j<resultEleList.size();j++){oneReuslt=new Result();resultEle=resultEleList.get(j);System.out.println(" 视图结果:"+resultEle.attributeValue("name"));System.out.println(" 是否重定向:"+resultEle.attributeValue("redirect"));System.out.println(" 视图名称:"+resultEle.getText());oneReuslt.setName(resultEle.attributeValue("name"));oneReuslt.setIsRedirect(resultEle.attributeValue("redirect"));oneReuslt.setUrl(resultEle.getText());resultMap.put(oneReuslt.getName(), oneReuslt);}oneAction.setResultMap(resultMap);actionMap.put(oneAction.getActionName(), oneAction);}System.out.println("=================map获取之后==============");ActionMapping regAction=actionMap.get("Register");System.out.println(" 名称"+regAction.getActionName());System.out.println(" 类名:"+regAction.getClassName());System.out.println(" 对应的result:"+regAction.getResultMap());} catch (DocumentException e) {e.printStackTrace();}}}====================运行结果==================
名称:Login类名:com.obtk.actions.LoginAction================= 视图结果:success 是否重定向:false 视图名称:index.jsp 视图结果:input 是否重定向:true 视图名称:Login.jsp名称:Register类名:com.obtk.actions.RegisterAction================= 视图结果:success 是否重定向:false 视图名称:Login.jsp 视图结果:input 是否重定向:true 视图名称:Register.jsp=================map获取之后============== 名称Register 类名:com.obtk.actions.RegisterAction 对应的result:{input=com.obtk.entity.Result@6c9f0f, success=com.obtk.entity.Result@13ec758}
如果对您有帮助,请点个赞!!
- 使用Dom4j解析XML文档
- 使用dom4j解析xml文档
- 使用Dom4j解析XML文档
- 使用dom4j解析XML文档
- 使用dom4j解析XML文档
- 使用Dom4j解析XML文档
- 使用dom4j解析xml文档
- 使用DOM4J解析xml文档
- 使用SAX和dom4j解析XML文档
- XML文档解析包 Dom4j 简单使用
- 使用dom4j生成和解析XML文档
- Dom4j解析XML文档
- dom4j解析xml文档
- DOM4J解析XML文档
- dom4j解析xml文档
- DOM4J解析XML文档
- DOM4J解析XML文档
- Dom4J解析XML文档
- JVM规范一(简介)
- 由C++类指针初始化引起的问题汇总
- 1014. 福尔摩斯的约会 (20)
- Python笔记之Django命令大全
- Swift String.Index 字符串的切片操作
- 使用dom4j解析xml文档
- 触发器
- ubuntu14.04 安装jdk 的两种方式
- LeetCode week 6 : Jump Game
- Spring
- 面向对象三大特性之多态
- RocketMQ源码解析-Consumer启动(1)
- MySQL学习笔记——慢查询
- 欢迎使用CSDN-markdown编辑器