转自:https://www.aliyun.com/jiaocheng/1339446.html
xml文件:
<?xml version="1.0" encoding="GBK"?> <phoneinfo>
<brand name="小米" id="1">
<type name="MI5"/>
<type name="NOTE"/>
<type name="MI5S"/>
</brand>
<brand name="华为" id="2">
<type name="荣耀8"/>
<type name="荣耀8plus"/>
</brand>
<brand name="苹果" id="3">
<type name="iphone7"/>
<type1 name="iphone6"/>
</brand>
</phoneinfo> 用dom4j解析xml文件
package com.hfxt; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
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; //dom4j解析xml并且执行(增、删、改、查)操作
public class dom4jXmlDemo { private Document document; public static void main(String[] args){
dom4jXmlDemo domXml = new dom4jXmlDemo();
domXml.getDom();
domXml.addPhoneInfo();
domXml.updatePhoneInfo();
//domXml.deletePhoneInfo();
domXml.showPhoneInfo(); } //获取DOM树
public void getDom(){
SAXReader reader = new SAXReader();
try {
document = reader.read(new File("phoneinfo.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
} //增加手机信息
public void addPhoneInfo(){
//获取根节点元素
Element element = document.getRootElement();
//添加brand节点
Element brand = element.addElement("brand");
//为brand添加属性并且赋值
brand.addAttribute("name","苹果");
//添加type节点
Element type = brand.addElement("type");
Element type1 = brand.addElement("type1");
//为type节点添加name属性并且赋值
type.addAttribute("name","iphone7");
type1.addAttribute("name","iphone6");
saveXml("phoneinfo.xml");
} //删除手机信息
public void deletePhoneInfo(){
//获取根节点
Element element = document.getRootElement();
Iterator brand = element.elementIterator();
//遍历获取每个节点信息
while(brand.hasNext()){
Element eleBrand = (Element)brand.next();
//删除name为苹果的brand
if(eleBrand.attributeValue("name").equals("苹果")){
eleBrand.getParent().remove(eleBrand);
}
}
//保存信息
saveXml("phoneinfo.xml");
} //修改手机信息
public void updatePhoneInfo(){
//获取手机信息
Element element = document.getRootElement();
Iterator brand = element.elementIterator();
int id = 0;
while(brand.hasNext()){
Element eleBrand = (Element)brand.next();
id++;
eleBrand.addAttribute("id",id+"");
}
saveXml("phoneinfo.xml");
} //显示手机信息
public void showPhoneInfo(){
//获取根节点
Element element = document.getRootElement();
Iterator brand = element.elementIterator();
while(brand.hasNext()){
Element eleBrand = (Element)brand.next();
//根据name属性获取对应的值的名字
System.out.println(eleBrand.attributeValue("name"));
//获取type节点
Iterator type = eleBrand.elementIterator();
//遍历所有type节点
while(type.hasNext()){
Element eleType = (Element)type.next();
System.out.println("\t"+eleType.attributeValue("name"));
}
}
} //保存信息到xml
public void saveXml(String path){
//格式化
OutputFormat format = OutputFormat.createPrettyPrint();
//指定字符编码格式
format.setEncoding("GBK");
XMLWriter writer = null;
//将文件按照指定格式输出到指定路径中
try {
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)),format);
writer.write(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭服务
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
- 用dom4j解析xml文件并执行增删改查操作