`

dom4j解析XML

    博客分类:
  • J2SE
阅读更多
package com.test;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.File;
import java.io.FileWriter;

public class TestDom4j {

	public static void main(String[] args) {
		try {
			Document document = DocumentHelper.createDocument();
			
			document.addComment("create by luodada");
			
			Element company = document.addElement("company");
			Element department = company.addElement("department");
			Element employee = department.addElement("employee");
			
			company.addAttribute("companyID", "007");
			department.addAttribute("deptName", "Sale");
			department.addAttribute("count", "20");
			employee.addAttribute("name", "kobe");
			employee.addAttribute("age", "31");
			
			File file = new File("c:/dom4j.xml");
			FileWriter fw = new FileWriter(file);
			document.write(fw);
			fw.close();
			System.out.println("Over......");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics