导入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);
}
}
}
}