基于Dom4j操作XML

Dom4j下载地址
链接:https://pan.baidu.com/s/1TYk-ZWVpJymuxE5a7JKmyw
提取码:b604
建立xml文件 phone.xml

<?xml version="1.0" encoding="GBK"?>
<PhoneInfo> 
  <Brand name="华为"> 
    <Type name="U8650"/>  
    <Type name="HW123"/>  
    <Type name="HW321"/> 
  </Brand>  
  <Brand name="苹果"> 
    <Type name="iphone8"/> 
  </Brand>  
  <Brand name="oppo"> 
    <Type name="oppor17"/> 
  </Brand>   
</PhoneInfo>

导入dm4j.jar包及xml文件
在这里插入图片描述
代码实现

package com.offcn.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ConfigUtils {
	//测试代码
	public static void main(String[] args) {
		ConfigUtils configUtils = new ConfigUtils();
		configUtils.getDoc("src/phone.xml");
		configUtils.add();
		configUtils.delete();
		configUtils.update();
		configUtils.save("src/phone.xml");
		configUtils.showInfo();
	}
	
	private Document doc;
	
	//获取dom对象
	public void getDoc(String path){
		SAXReader reader = new SAXReader();
		try {
			doc = reader.read(new File(path));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	//展示所有信息
	public void showInfo(){
		//获取根元素
		Element rootele = doc.getRootElement();
		Iterator<?> iterator = rootele.elementIterator();
		while(iterator.hasNext()){
			Element parentele = (Element)iterator.next();
			System.out.println(parentele.attributeValue("name"));
			//得到子元素的迭代器
			Iterator<?> iterator2 = parentele.elementIterator();
			while(iterator2.hasNext()){
				Element childele = (Element)iterator2.next();
				System.out.println(childele.attributeValue("name"));
			}
		}
	}
	//创建一个节点
	public void add(){
		//得到根节点
		Element rootele = doc.getRootElement();
		//创建一个elementBrand节点
		Element elementBrand = rootele.addElement("Brand");
		//设置属性
		elementBrand.addAttribute("name", "oppo");
		//创建elementType节点
		Element elementType = elementBrand.addElement("Type");
		//设置属性
		elementType.addAttribute("name", "oppor17");
	}
	//保存到源文件
	public void save(String path){
		try {
			//传入文件路径
			FileWriter file = new FileWriter(path);
			//格式化
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("GBK");
			
			XMLWriter writer = new XMLWriter(file,format);
			writer.write(doc);
			
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//删除节点
	public void delete(){
		//获取根元素
		Element rootele = doc.getRootElement();
		Iterator<?> iterator = rootele.elementIterator();
		while(iterator.hasNext()){
			Element parentele = (Element)iterator.next();
			if(parentele.attributeValue("name").equals("oppor")){
				parentele.getParent().remove(parentele);
			}
		}
	}
	//更新
	public void update(){
		//获取根元素
		Element rootele = doc.getRootElement();
		Iterator<?> iterator = rootele.elementIterator();
		int i = 1;
		while(iterator.hasNext()){
			Element parentele = (Element)iterator.next();
			parentele.addAttribute("id", i+1+"");
			i++;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值