欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dom4j操作xml

程序员文章站 2022-05-28 20:28:22
...
package com.zl;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Iterator;
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.SAXReader;
import org.dom4j.io.XMLWriter;
 

public class TestDom4J {
	/**
	 * 得到Document
	 * @param file
	 * @return
	 * @throws Exception
	 */
	private Document getDocument(File file) throws Exception {
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		return doc;
	}
	/**
	 * 读取XML
	 * @param fileName
	 */
	private void readXml(String  fileName) {
		try {
			File file=new File(fileName);
			Document doc = getDocument(file);
			Element root = doc.getRootElement();
			for (Iterator it = root.elementIterator(); it.hasNext();) {
				Element el = (Element) it.next();
				String student=el.getName();
				String name=el.element("name").getText();
				String sex=el.elementText("sex");
				System.out.println(student+"-"+name+"-"+sex);
			}
		 

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 建立一个新的XML
	 * @param fileName
	 * @throws Exception
	 */
	private void creatXML(String fileName) throws Exception {
		 Document doc=DocumentHelper.createDocument();
		 Element root=doc.addElement("persons");
		 Element st=root.addElement("student");
		 st.addAttribute("id", "1");
		 Element stn=st.addElement("name");
		 stn.setText("小明");
		 Element stg=st.addElement("sex");
		 stg.setText("女");
		 Element pel=root.addElement("person");
		 pel.addAttribute("id", "1");
		 Element name=pel.addElement("name");
		 name.setText("zl");
		 Element gender=pel.addElement("gender");
		 gender.setText("男");
		 Writer out=new FileWriter(fileName);
		 OutputFormat outf=OutputFormat.createPrettyPrint();
		 outf.setEncoding("GBK");
		 XMLWriter writer=new XMLWriter(out,outf);
		 writer.write(doc);
		 writer.close();
		 System.out.println("生成成功");

	}
	
	/**
	 * 添加XML中的一个元素
	 * @param file
	 * @throws Exception
	 */
	private void addElement(File file) throws Exception {
		 
		Document doc=getDocument(file);
		Element root=doc.getRootElement();
		Element man=root.addElement("man");
		man.addAttribute("id", "1");
		OutputFormat format=new OutputFormat();
		format.setEncoding("GBK");
		XMLWriter writer=new XMLWriter(new FileWriter(file),format);
		writer.write(doc);
		writer.close();
		System.out.println("OK");
		

	}
	
	private void delete(File	 file) throws Exception {
		 Document doc=getDocument(file);
		 Element root =doc.getRootElement();
		 List<Element> list=root.elements("student");
		 for (Element el: list) {
			if (el.attributeValue("id").equals("1")) {
				root.remove(el);	 
				XMLWriter writer=new XMLWriter(new FileWriter(file));
				writer.write(doc);
				writer.close();
				System.out.println("OK");
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		TestDom4J t=new TestDom4J();
		//t.modifyElment(new File("zl.xml"));
		t.creatXML("zl.xml");
		//t.readXml("struts.xml");
		//t.delete(new File("zl.xml"));
 
		 
	}
}

 

相关标签: XML Struts