dom 修改xml java_java dom 修改xml

给一个空的xml文件写入数据

package com.softeem.dom.write;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;

public class Demo1 {

public static void main(String[] args) {

// 增加文档

Document document = DocumentHelper.createDocument();

// 增加Element: parent.addElement("childName");

Element root = document.addElement("userlist");

// 增加Attribute addAttribute("属性名","属性值")

Element user1 = root.addElement("user");

user1.addAttribute("id", "100");

Element username = user1.addElement("username");

username.setText("小周");

Element password = user1.addElement("password");

password.setText("admin");

// 写入文件

XMLWriter xmlWriter = null;

FileOutputStream fos = null;

try {

fos = new FileOutputStream(new File("src\\WriteDemo.xml"));

// 设置输出格式

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("utf-8");

// 创建writer(参数一:输出流,参数二L:输出格式)

xmlWriter = new XMLWriter(fos, format);

// 写数据

xmlWriter.write(document);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

// 关闭资源

fos.close();

xmlWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

package com.softeem.dom.write;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;

public class Demo2 {

public static void main(String[] args) {

List userList = new ArrayList();

ArrayList favoriteList = new ArrayList();

favoriteList.add("看电影");

favoriteList.add("美食");

favoriteList.add("旅游");

userList.add(new UserBean("100", "aaa", "aaa", favoriteList));

userList.add(new UserBean("101", "bbb", "bbb", favoriteList));

userList.add(new UserBean("102", "ccc", "ccc", favoriteList));

save(userList);

}

private static void save(List userList) {

// 1.创建document

Document document = DocumentHelper.createDocument();

// 2.将数据加入到document

//

Element root = document.addElement("userlist");

for (UserBean user : userList) {

//

Element userEle = root.addElement("user");

userEle.addAttribute("id", user.getId());

// 文本

Element nameEle = userEle.addElement("username");

nameEle.setText(user.getUsername());

// 文本

Element pwdEle = userEle.addElement("password");

pwdEle.setText(user.getPassword());

// ...

Element favoriteListEle = userEle.addElement("favoritelist");

List favorites = user.getFavorites();

for (String favorite : favorites) {

//

Element favoriteEle = favoriteListEle.addElement("favorite");

favoriteEle.setText(favorite);

}

}

// 3.将document持久化

FileOutputStream fos = null;

XMLWriter writer = null;

try {

fos = new FileOutputStream(new File("src\\UserList.xml"));

OutputFormat outputFormat = OutputFormat.createPrettyPrint();

// 编码方式:<?xml version="1.0" encoding="utf-8"?>

outputFormat.setEncoding("utf-8");

writer = new XMLWriter(fos, outputFormat);

writer.write(document);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (writer != null) {

writer.close();

}

if (fos != null) {

fos.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

修改属性值

package com.softeem.dom.write;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import org.dom4j.Attribute;

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;

import com.softeem.utils.StreamUtils;

import com.softeem.utils.XmlUtils;

public class Demo3 {

public static void main(String[] args) throws DocumentException {

// 拿到属性对象,修改值

SAXReader saxReader = new SAXReader();

Document doc = saxReader.read(new File("src\\UserList.xml"));

// 获取根节点

Element root = doc.getRootElement();

// 取user元素

Element userEle = root.element("user");

// 修改属性:

/*

* 方式一:获取属性对象,修改属性值 // 获取属性对象 Attribute idAttr =

* userEle.attribute("id"); // 修改值 if (idAttr != null)

* idAttr.setValue("1000000");

*/

// 方式二:添加同名属性(如果属性不存在,添加;存在就覆盖)

userEle.addAttribute("id", "200000");

// 修改Element的text

// (1)找到Element

Element pwdEle = userEle.element("password");

// (2)重新设置文本

pwdEle.setText("123456789");

// 保存修改(修改的都是document对象,对文件内容没有任何影响)

XmlUtils.writeDocument(doc, new File("src\\UserList.xml"));

}

///**

// * 将document中的数据持久化到文件中

// *

// * @param source

// * @param targetFile

// */

//public static void writeDocument(Document source, File targetFile) {

//FileOutputStream fos = null;

//XMLWriter xmlWriter = null;

//try {

//fos = new FileOutputStream(targetFile);

//OutputFormat format = OutputFormat.createPrettyPrint();

//format.setEncoding("utf-8");

//

//xmlWriter = new XMLWriter(fos, format);

//

//xmlWriter.write(source);

//} catch (FileNotFoundException e) {

//e.printStackTrace();

//} catch (UnsupportedEncodingException e) {

//e.printStackTrace();

//} catch (IOException e) {

//e.printStackTrace();

//} finally {

//try {

//if (xmlWriter != null)

//xmlWriter.close();

//} catch (IOException e) {

//e.printStackTrace();

//}

//

//StreamUtils.closeOut(fos);

//}

//}

}

删除元素

package com.softeem.dom.write;

import java.io.File;

import java.util.Iterator;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import com.softeem.utils.XmlUtils;

public class Demo4 {

public static void main(String[] args) throws DocumentException {

// 删除Element:删除id=101的user元素

deleteUserById("101");

// 删除属性 : 删除所有user的id属性

deleteIdAttribute();

}

private static void deleteIdAttribute() throws DocumentException {

SAXReader reader = new SAXReader();

Document doc = reader.read(new File("src\\UserList.xml"));

// 获取root

Element root = doc.getRootElement();

// 获取user

Iterator userIte = root.elementIterator("user");

while (userIte.hasNext()) {

Element userEle = userIte.next();

// 需要把userEle中的id属性删除

// 获取属性对象

Attribute idAttr = userEle.attribute("id");

idAttr.detach();

// 方式二:

// idAttr.getParent().remove(idAttr);

}

//持久化

XmlUtils.writeDocument(doc, new File("src\\UserList.xml"));

}

public static void deleteUserById(String id) throws DocumentException {

SAXReader reader = new SAXReader();

Document doc = reader.read(new File("src\\UserList.xml"));

// 获取root

Element root = doc.getRootElement();

// 获取user

Iterator userIte = root.elementIterator("user");

while (userIte.hasNext()) {

Element userEle = userIte.next();

if (id.equals(userEle.attributeValue("id"))) {

// 删除

// 方式一:先获取Element对象,调用detach()

// userEle.detach();

// 方式二:使用parent.remove(child);

userEle.getParent().remove(userEle);

}

}

// 持久化

XmlUtils.writeDocument(doc, new File("src\\UserList.xml"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值