dom4j(1)

package com.meizu58.dom4j;

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

import org.dom4j.Attribute;
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 {
//读取xml ,解析xml。
readDocument();
//创建xml

// createDocument();
}



public static void readDocument() throws Exception{
SAXReader reader = new SAXReader();
      Document document = reader.read(new File("Student.hbm.xml"));
      Element rootElement = document.getRootElement();
//打印根节点
      p("rootElement.getName() = "+rootElement.getName());
     
      //准备打印子节点和子节点的属性
      for(Iterator i = rootElement.elementIterator();i.hasNext();){
     
      Element element = (Element)i.next();
      //打印根节点
      p("element.getName() = "+element.getName());
     
        //1  打印子节点的所有属性name和value
      for(Iterator j = element.attributeIterator();j.hasNext();){
    Attribute attribute = (Attribute)j.next();
    p(attribute.getName()+" = "+attribute.getValue());
      }
      }
      p("2xpath-----------------------------------------------------------");
      //2  xpath (为了方便记忆可以理解为类似sql的按条件查询)
      List<Node> list = document.selectNodes("//hibernate-mapping/class/property");
    
      for(Node n:list){
      p(n.getName());
      p("n.valueOf() = "+n.valueOf("@name"));
      //得到xml里文本值 getText()
      p("n.getText() = "+n.getText());
   
      }
     
      //读取指定到属性的元素
      String a = document.selectSingleNode("//hibernate-mapping/class/property[@type='name2']").getText();
          p("a = "+a);
}

public static Document createDocument() throws Exception{
Document document = DocumentHelper.createDocument();
Element root = document.addElement("hibernamet-mapping");
Element classElement = root.addElement("class").addAttribute("class", "com.meizu58.Student").addAttribute("table", "t_student");
classElement.addElement("property").addAttribute("name", "username");
//设置xml里的文本值setText
classElement.addElement("property").addAttribute("name", "password").setText("123456789");
OutputFormat format = OutputFormat.createPrettyPrint();
//XMLWriter writer = new XMLWriter(new FileWriter("Student2.hbm.xml"),format);
//解决了由于编码(上面的为GBK,tld头文件确是Utf-8)不能解析的问题,现在统一为utf-8
XMLWriter writer = new XMLWriter(new FileOutputStream("Student2.hbm.xml"), format); writer.write(document);
writer.close();
return document;
}


//公共调用的输出方法
public static void p(Object o){
System.out.println(o);
}

}

student.hbm.xml




<hibernate-mapping>
<class name="com.meizu58.Student" table="t_student">
<property name="username" type="name1">name1的value</property>
<property name="username" type="name2">name2的value</property>
<property name="password"></property>
<property name="赵四name">赵四value</property>
</class>
</hibernate-mapping>

你可能感兴趣的:(sql,xml,Hibernate,J#)