使用Dom4j读取指定文件,并写入指定文件
导入dom4j的包,然后写入代码如下
package com.dom4j.cn; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class ChangeXML { /** * 把文件读取并且写到指定文件中 * @throws Exception */ @Test public void changeXML() throws Exception { Document document = new SAXReader().read("./src/test.xml"); //修改属性id document.getRootElement().element("shaoxin").attribute("id").setValue("003"); //删除属性id为002的节点 Element element =(Element) document.getRootElement().elements().get(1); element.detach(); FileOutputStream fileOutputStream = new FileOutputStream("C:/Users/shaoxin/Desktop/a.xml"); OutputFormat outputFormat = new OutputFormat().createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(fileOutputStream,outputFormat); outputFormat.setEncoding("utf-8"); xmlWriter.write(document); fileOutputStream.close(); } }
package com.dom4j.cn; 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; import org.junit.Test; public class Dom_ChangeXML { /** * 创建各个节点并且保存在指定文件中 */ @Test public void changeXML() { Document document = DocumentHelper.createDocument(); Element rootElement = document.addElement("people"); Element shaoxin = rootElement.addElement("shaoxin"); Element age = shaoxin.addElement("age"); Element name = shaoxin.addElement("name"); shaoxin.addAttribute("id", "001"); age.addText("21"); name.addText("邵欣"); FileOutputStream fileOutputStream; try { fileOutputStream = new FileOutputStream("C:/Users/shaoxin/Desktop/a.xml"); OutputFormat outputFormat = new OutputFormat().createPrettyPrint(); outputFormat.setEncoding("utf-8"); XMLWriter xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); fileOutputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.dom4j.cn; import java.io.File; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class Dom_XML { /** * 输出文件中所有的节点名称 * @param args */ public static void main(String[] args) { SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(new File("./src/test.xml")); Element element = document.getRootElement(); sax(element); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void sax(Element element) { System.out.println(element.getName()); Iterator<Node> node = element.nodeIterator(); while(node.hasNext()){ Node node2 = node.next(); if (node2 instanceof Element) { Element element2 = (Element) node2; sax(element2); } } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步