dom4j创建和解析xml

1:引入dom4j.jar包

免费下载:  http://ishare.iask.sina.com.cn/f/8133067.html?fr


2:创建接口
package xml;
/**
*
* @author hongliang.dinghl
* 定义XML文档建立与解析的接口
*/
public interface XmlDocument {
/**
* 建立XML文档
* @param fileName 文件全路径名称
*/
public void createXml(String fileName);
/**
* 解析XML文档
* @param fileName 文件全路径名称
*/
public void parserXml(String fileName);
}
3:完成实现类
package xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
 *
 * @author hongliang.dinghl Dom4j 生成XML文档与解析XML文档
 */
public class Dom4jDemo implements XmlDocument {
    public void createXml(String fileName) {
        Document document = DocumentHelper.createDocument();
        Element employees = document.addElement("employees");
        Element employee = employees.addElement("employee");
        Element name = employee.addElement("name");
        name.setText("ddvip");
        Element sex = employee.addElement("sex");
        sex.setText("m");
        Element age = employee.addElement("age");
        age.setText("29");
        try {
            Writer fileWriter = new FileWriter(fileName);
            XMLWriter xmlWriter = new XMLWriter(fileWriter);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    public void parserXml(String fileName) {
        File inputXml = new File(fileName);
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputXml);
            Element employees = document.getRootElement();
            for (Iterator i = employees.elementIterator(); i.hasNext();) {
                Element employee = (Element) i.next();
                for (Iterator j = employee.elementIterator(); j.hasNext();) {
                    Element node = (Element) j.next();
                    System.out.println(node.getName() + ":" + node.getText());
                }
            }
        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("dom4j parserXml");
    }
}

4:在指定的盘符下创建指定的xml文件(这里我采用的是回写文件的方法,可以换成文件创建的方法,就是说如果所有回写的xml不存在那么就创建xml在回写内容。)

当指定文件不存在时创建文件:

FileOutputStream fileOutputStream = new FileOutputStream(fileName);

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");

OutputFormat fmt = OutputFormat.createPrettyPrint();

fmt.setEncoding("utf-8");

XMLWriter writer = new XMLWriter(outputStreamWriter,fmt);

writer.write(document);

writer.close();



5:构建测试类
package xml;
public class TestMain {
                                  
    XmlDocument xmlEbi = new Dom4jDemo();
    public void testCreate(){
        xmlEbi.createXml("E:/xml/create.xml");
    }
    public void testParser(){
        xmlEbi.parserXml("E:/xml/create.xml");
    }
    public static void main(String[] args) {
        TestMain tm = new TestMain();
        tm.testCreate();
        tm.testParser();
    }
}


你可能感兴趣的:(接口,import,public,interface)