一、依赖
org.dom4jdom4j2.1.3compile
二、待操作XML文件
/list.jspshowUserinfoList.ghy/showUserinfoList.jsp
三、操作节点
package org.example.xmltest;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 java.io.FileWriter;
import java.io.IOException;
import java.util.List;public class Delete {public static void main(String[] args) throws DocumentException, IOException {SAXReader reader = new SAXReader();//1、读取XML文件Document document = reader.read(reader.getClass().getResourceAsStream("/struts.xml"));//2、获取根元素Element rootElement = document.getRootElement();//获取根元素的所有子元素List rootChildren = rootElement.elements();for (Element child : rootChildren) {List childChildren = child.elements();if (childChildren != null && childChildren.size() > 0) {for (Element childChild : childChildren) {//获取name属性Attribute name = childChild.attribute("name");if (name != null) {//如果属性存在,删除childChild.remove(name);}List elements = childChild.elements();for (Element element : elements) {//删除子元素childChild.remove(element);}}}}//创建输出格式OutputFormat format = OutputFormat.createPrettyPrint();//XML写入工具XMLWriter writer = new XMLWriter(new FileWriter("delete.xml"), format);//写入文档至XML文件writer.write(document);//关闭流writer.close();}
}

四、删除后节点及属性后生成的文件
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!