前言
- 以下是本人根据网上和dom4j官网总结的xml的常用到的函数工具类。使用本工具类需要在项目中导入dom4j。
- 官网下载:https://dom4j.github.io
- 本版本中没有使用到Xpath的相关知识,也是后期待改进方面,根据对Xpath的粗浅认识,借助Xpath能够更方便的对XML中的元素进行定位。
- 而本工具类目前主要是通过父元素去根据子元素的名称和属性进行查找,对于多层次的XML来说,过于麻烦,但是一些结果较为简单的XML还是适用的。
- 本文主要分为四大块进行介绍。
创建的有关函数
创建一个XML文件的步骤:
(1)创建Document对象,该对象代表着整个XML文档
(2)创建root元素,XML文档可以视为在描述一棵树,而这颗树的顶端就是root元素。
(3)将root元素做父元素,添加子元素,可以理解为以树的顶端描绘一颗树。
(4)填充后,将Document对象写入到真实的XML中
创建Document对象
/***
* 创建Document 实例
* @return 返回Document 实例
*/
public static Document createDoc(){
// 创建一个Document实例
Document doc = DocumentHelper.createDocument();
return doc;
}
创建root元素
主要分为两种:一种主动设置root元素的名称,一种默认设置root名称为“root"。
/***
* 创建指定名称的根节点
* @param doc 要新增根节点的Document对象
* @param elName 根节点名称
* @return 根节点
*/
public static Element createRootEl(Document doc,String elName){
Element root = doc.addElement(elName);
return root;
}
/***
* 创建名称为“root"的根节点
* @param doc 要新增根节点的Document对象
* @return 根节点
*/
public static Element createRootEl(Document doc){
Element root = doc.addElement("root");
return root;
}
创建子元素
在父元素下创建子元素。预设函数有6种,主要应用的是前三种,直接使用字符串来创建,后面三种涉及Attribute以及Text,这些是结果,无法直接创建对象,暂时没有找到如何实例化,目前是暂时备着。
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param attName 新增元素的Attribute名称
* @param attValue 新增元素的Attribute值
* @param textCon 新增元素的文本内容
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,String attName,String attValue,String textCon){
Element el=father.addElement(elName);
el.addAttribute(attName,attValue);
el.addText(textCon);
return el;
}
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param attName 新增元素的Attribute名称
* @param attValue 新增元素的Attribute值
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,String attName,String attValue){
Element el=father.addElement(elName);
el.addAttribute(attName,attValue);
return el;
}
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param textCon 新增元素的文本内容
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,String textCon){
Element el=father.addElement(elName);
el.addText(textCon);
return el;
}
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param att 新增元素的Attribute
* @param textCon 新增元素的文本
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,Attribute att,Text textCon){
Element el=father.addElement(elName);
el.add(att);
el.add(textCon);
return el;
}
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param att 新增元素的Attribute
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,Attribute att){
Element el=father.addElement(elName);
el.add(att);
return el;
}
public static Element createEl(Element father,String elName,ArrayList<Attribute> attList){
Element el=father.addElement(elName);
for(Attribute att: attList){
el.add(att);
}
return el;
}
/***
* 创建元素
* @param father 要新增元素的父元素
* @param elName 新增元素的名称
* @param textCon 新增元素的文本
* @return 返回创建的元素
*/
public static Element createEl(Element father,String elName,Text textCon){
Element el=father.addElement(elName);
el.add(textCon);
return el;
}
建立两个元素的依赖关系
/***
* 给元素添加子元素
* @param father 父元素
* @param child 子元素
*/
public static void addEl(Element father,Element child){
father.add(child);
}
将Document对象写入XML中
/***
* 将Document对象写入xml文件中
* @param doc 要写的Document对象
* @param xmlPath xml文件路径
* @return 返回成功与否
*/
public static boolean WriteXml(Document doc,String xmlPath){
// 自定义xml样式
OutputFormat format = new OutputFormat();
format.setIndentSize(2); // 行缩进
format.setNewlines(true); // 一个结点为一行
format.setTrimText(true); // 去重空格
format.setPadText(true);
format.setNewLineAfterDeclaration(false); // 放置xml文件中第二行为空白行
// 输出xml文件
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileOutputStream(new File(xmlPath)), format);
writer.write(doc);
writer.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
}
}
查询的相关函数
获取Document对象
获取指定路径下的xml的Document对象
/***
* 获取指定文件路径下的Document对象
* @param xmlPath xml文件路径
* @return 返回Document对象
* @throws DocumentException
*/
public static Document getDoc(String xmlPath) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(xmlPath);
return document;
}
获取Root元素
/***
* 获取Root元素
* @return 返回Root元素
*/
public static Element getRootEl(Document doc){
Element root = doc.getRootElement();
return root;
}
遍历获取元素
主要有三种方法:
- 通过父元素遍历所有子元素
- 通过父元素遍历所有指定名称的子元素
- 通过父元素列表遍历所有指定名称的子元素
/***
* 遍历得到指定元素下的下一级子元素
* @param father 指定的父元素
* @return 返回遍历得到的子元素列表
*/
public static ArrayList<Element> traverseEL(Element father){
ArrayList<Element> elRList=new ArrayList<>();
for (Iterator<Element> it = father.elementIterator(); it.hasNext();) {
Element element = it.next();
elRList.add(element);
}
return elRList;
}
/***
* 遍历得到父元素father下所有名称为elName的元素
* @param father 父元素
* @param elName 查找的元素名称
* @return 父元素father下所有名称为elName的元素列表
*/
public static ArrayList<Element> traverseELByName(Element father,String elName){
ArrayList<Element> elRList=new ArrayList<>();
for (Iterator<Element> it = father.elementIterator(elName); it.hasNext();) {
Element element = it.next();
elRList.add(element);
}
return elRList;
}
/***
* 遍历得到父元素列表fatherList中父元素下所有名称为elName的元素
* @param fatherList 父元素列表
* @param elName 查找的元素名称
* @return 父元素列表fatherList下所有名称为elName的元素列表
*/
public static ArrayList<Element> traverseElListByName(ArrayList<Element> fatherList,String elName){
ArrayList<Element> elRList=new ArrayList<>();
for(Element father:fatherList){
for (Iterator<Element> it = father.elementIterator(elName); it.hasNext();) {
Element element = it.next();
elRList.add(element);
}
}
return elRList;
}
查找元素
主要分为两种:
- 通过父元素和子元素的名称查找匹配的子元素列表
- 通过父元素和子元素的属性查找匹配的子元素列表
/***
* 提供父元素查找子元素,根据子元素name查找元素
* @param father 父元素
* @param ElName 子元素name
* @return 找到返回指定子元素列表,未找到返回的列表长度为0
*/
public static ArrayList<Element> findElByName(Element father,String ElName){
ArrayList<Element> ElList=new ArrayList<>();
for (Iterator<Element> it = father.elementIterator(); it.hasNext();) {
Element element = it.next();
String name=element.getName();
if(name.equals(ElName)){
ElList.add(element);
}
}
return ElList;
}
/***
* 提供父元素查找单个子元素,根据子元素的Attribute Name和Attribute value查找元素
* @param father 父元素
* @param attName 子元素的Attribute Name
* @param attKeyValue 子元素的Attribute value
* @return 找到返回指定子元素列表,未找到返回的列表长度为0
*/
public static ArrayList<Element> findElByAtt(Element father,String attName,String attKeyValue){
ArrayList<Element> ElList=new ArrayList<>();
for (Iterator<Element> it = father.elementIterator(); it.hasNext();) {
Element element = it.next();
String vlaue=element.attributeValue(attName);
if(vlaue.equals(attKeyValue)){
ElList.add(element);
}
}
return ElList;
}
删除的相关函数
删除元素
删除父元素中某个子元素
/***
* 删除父元素下的子元素
* @param father 父元素
* @param child 子元素
* @return 成功返回true,失败返回false
*/
public static boolean removeEl(Element father,Element child){
try{
father.remove(child);
return true;
}catch (Exception e){
return false;
}
}
修改
给元素添加属性
一个元素可以有多个属性
/***
* 给元素添加属性
* @param el 元素
* @param attName 属性名称
* @param Value 属性值
* @return 成功返回true,失败返回false
*/
public static boolean addElAtt(Element el,String attName,String Value){
try{
el.addAttribute(attName,Value);
return true;
}catch (Exception e){
return false;
}
}
更改元素属性值
该函数所有的API目前已经取消了,但是还能实现,只是暂时没有找到其他实现的方式
/***
* 更改元素的属性值
* @param el 元素
* @param attName 元素属性名称
* @param newValue 元素属性新的值
* @return 成功返回true,失败返回false
*/
public static boolean updateAtt(Element el,String attName,String newValue){
try{
el.setAttributeValue(attName,newValue);
return true;
}catch (Exception e){
return false;
}
}
更改元素的Text
/***
* 更改元素的text
* @param el 元素
* @param text 新的text值
* @return 成功返回true,失败返回false
*/
public static boolean updateText(Element el,String text){
try{
el.setText(text);
return true;
}catch (Exception e){
return false;
}
}
最后,附上工具类的链接










网友评论