使用Dom4j解析和创建XML文件

参考文章:http://www.blogjava.net/i369/articles/154264.html

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;


import org.dom4j.Attribute;//需要导入dom4j的jar
import org.dom4j.Document;
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();
       p(element.getName());
       
       for ( Iterator j = element.attributeIterator(); j.hasNext(); ) {
           Attribute attribute = (Attribute) j.next();
           p(attribute.getName() + "-" + attribute.getValue());
        }
       for ( Iterator k = element.elementIterator(); k.hasNext(); ) {
         Element selement = (Element) k.next();
         p(selement.getName());
         p("~~~~~~~~~~~~~~~~~~~~~~~~~");
         for ( Iterator l = selement.attributeIterator(); l.hasNext(); ) {
             Attribute lattribute = (Attribute) l.next();
             p(lattribute.getName() + "-" + lattribute.getValue());
          }
        }
    }
   
   //xpath,通过xpath解析需要导入jaxen架包
   List list = document.selectNodes( "//hibernate-mapping/class/property" );
   for(Node n : list) {
    System.out.println(n.getName());
    p(n.valueOf("@name"));
   }
           
   createDocument();      
}
//创建xml文件
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");
      
      OutputFormat format = OutputFormat.createPrettyPrint();
      XMLWriter writer = new XMLWriter(
              new FileWriter( "User1.hbm.xml" ), format
          );
          writer.write( document );
          writer.close();
      
      return document;
   }
public static void p(Object o) {
System.out.println(o);
}

}


解析的xml文件User.hbm.xml:








创建的xml文件User1.hbm.xml:


 
   
 




程序运行结果:
hibernate-mapping
class
name-com.bjsxt.User
table-t_user
property
~~~~~~~~~~~~~~~~~~~~~~~~~
name-username
property
~~~~~~~~~~~~~~~~~~~~~~~~~
name-password
property
username
property
password

你可能感兴趣的:(Java,web)