[Java] XML 解析 与 Dom4j 的使用

DOM4JTest.java

package com.bjsxt.dom4j;

import java.io.File;
import java.io.FileWriter;
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.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class DOM4JTest {
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("User.hbm.xml"));
        Element rootElement = document.getRootElement();
        p(rootElement.getName());

        // 枚举所有子节点
        for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();
            // do something
            p(element.getName());

            // 枚举属性
            for (Iterator j = element.attributeIterator(); j.hasNext();) {
                Attribute attribute = (Attribute) j.next();
                // do something
                p(attribute.getName() + "-" + attribute.getValue());
            }
        }
        // xpath = xml+path
        List<Node> list = document.selectNodes("//hibernate-mapping/class/property");
        //Node node = document.selectSingleNode("//hibernate-mapping/class/property"); 只拿第一个
        //String name = node.valueOf( @name );
        for (Node n : list) {
            p(n.getName());
            p(n.valueOf("@name"));
        }
        createDocument();
        
    }

    public static void p(Object o) {
        System.out.println(o);
    }
    public static Document createDocument() throws Exception {
           Document document = DocumentHelper.createDocument();
           Element root = document.addElement("hibernate-mapping");
           Element classElement = root.addElement("class")
                   .addAttribute("name", "com.bjsxt.User")
                   .addAttribute("table", "t_user");
           classElement.addElement("property").addAttribute("name", "username");
           /*
           FileWriter out = new FileWriter("User1.hbm.xml");
           document.write(out);
           out.flush();
           out.close();
           */
           // 美化格式
           OutputFormat format = OutputFormat.createPrettyPrint();
           XMLWriter writer = new XMLWriter(
                   new FileWriter( "User2.hbm.xml" ), format
               );
               writer.write( document );
               writer.close();
           
           return document;
        }
     
}
User.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.User" table="t_user">
		<property name="username"></property>
		<property name="password"></property>
	</class>
</hibernate-mapping>
User2.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<hibernate-mapping>
  <class name="com.bjsxt.User" table="t_user">
    <property name="username"/>
  </class>
</hibernate-mapping>
--

---

posted @ 2014-01-12 00:10  小尼人00  阅读(146)  评论(0编辑  收藏  举报