使用dom4j对xml的读写修改,首先还是需要的jar
此处使用的jar版本是dom4j-1.6.1.jar ,此外使用dom4j还需要另外一个jar就是jaxen-1.1-beta-6.jar,否则使用将报错
下载地址http://download.youkuaiyun.com/download/liu119361940/5349921
个人感觉dom4j,使用起来应该更方便点,比jdom多点东西。。
ok,下面是代码:
package com.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlWriterAndReader {
/**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException, DocumentException {
// TODO Auto-generated method stub
// String path = "dom4j.xml";
// String pathEdit = "dom4jEdit.xml";
String path = "D:/dom4j.xml";
String pathEdit = "D:/dom4jEdit.xml";
createXml(path);
System.out.println("创建后直接读取》》》》》》》》》》》》》");
System.out.println("创建后直接读取》》》》》》》》》》》》》");
System.out.println("创建后直接读取》》》》》》》》》》》》》");
readXml(path);
System.out.println("修改后读取》》》》》》》》》》》》》");
System.out.println("修改后读取》》》》》》》》》》》》》");
System.out.println("修改后读取》》》》》》》》》》》》》");
editXml(path,pathEdit);
}
/**
* 创建xml
* @param path
* @throws IOException
*/
public static void createXml(String path) throws IOException{
//使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API 工厂类
Document doc = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("root");
doc.setRootElement(root);
//添加节点和属性
for(int i=0;i<2;i++){
Element user= root.addElement("user");
user.addEntity("name", "jack");
Element info= user.addElement("info");
info.addAttribute("sex", "mail"+1);
info.addAttribute("age", "1"+1);
info.addEntity("a", "a");
info.addText("bbb");
Element other= user.addElement("other");
other.addAttribute("address", "地址"+i);
other.setText("");
Element jianjie = user.addElement("jianjie");
jianjie.addText("一个人在战斗");
}
root.addAttribute("name", "value");
Element fff= root.addElement("fff");
fff.addAttribute("bbb", "ccc");
root.addText("txt");
root.addComment("comment");
root.addCDATA("cdata");
ProcessingInstruction pi = DocumentHelper.createProcessingInstruction("xml-stylesheet",
"href=\"bookList.html.xsl\" type=\"text/xsl\"");
root.add(pi);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlout = new XMLWriter(new FileOutputStream(path),format);
xmlout.write(doc);
xmlout.close();
}
/**
* 读取xml
* @param path
* @throws DocumentException
*/
public static void readXml(String path) throws DocumentException{
//使用 SAXReader 解析 XML 文档 catalog.xml
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File(path));
Element root = doc.getRootElement();
repeat(root);
}
/**
* 修改xml
* @param path
* @param editPath
* @throws DocumentException
* @throws IOException
*/
public static void editXml(String path,String editPath) throws DocumentException, IOException{
//使用 SAXReader 解析 XML 文档 dom4j.xml
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File(path));
Element root = doc.getRootElement();
// List<Element> user = root.selectNodes("//root/@user");
List<Element> users = root.selectNodes("user");
Iterator<Element> it = users.iterator();
while(it.hasNext()){ //对节点添加内容
Element u = it.next();
u.addAttribute("name2", "新加的名称2");
u.setText(u.getTextTrim()+"text也加啦");
}
List<Element> infos = doc.selectNodes("//user/info");
Iterator<Element> iit = infos.iterator();
while(iit.hasNext()){ //修改节点内容
Element info = iit.next();
info.attribute("sex").setText(info.attribute("sex").getText()+"-value");
info.attribute(1).setValue(info.attribute(1).getValue()+"-value");
}
List<Element> jianjies = doc.selectNodes("//jianjie");
Iterator<Element> iiit = jianjies.iterator();
while(iiit.hasNext()){
Element jianjie = iiit.next();
jianjie.setText(jianjie.getTextTrim()+"-到两个人的奋斗");
}
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlout = new XMLWriter(new FileOutputStream(editPath),format);
xmlout.write(doc);
xmlout.close();
//重新读取xml
Document doc2 = saxReader.read(new File(editPath));
Element root2 = doc2.getRootElement();
repeat(root2);
}
/**
* 递归取出xml中的值方法1
*/
public static void repeat(Element ele){
if(checkChild(ele)){
System.out.println("Element: " + ele.getName());
List<Attribute> attr = ele.attributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
System.out.println("Text1: " + ele.getTextTrim());
}
List<Element> list = ele.elements();
Iterator it = list.iterator();
while (it.hasNext()) {
Element element = (Element) it.next();
repeat(element);
}
}else{
System.out.println("Element: " + ele.getName());
System.out.println("Text2: " + ele.getTextTrim());
List<Attribute> attr = ele.attributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
}
}
}
/**
* 检测节点是否还有子节点
* @param element
* @return
*/
public static boolean checkChild(Element element){
if(!(element.elements()).isEmpty()){
return true;
}else{
return false;
}
}
}