package ren;
import java.io.FileOutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestJDom {
public static void main(String[] args) throws Exception{
String file = "c:\\lib.xml";
SAXBuilder sax= new SAXBuilder();
Document doc=sax.build(file);
Element root =doc.getRootElement();
// Element book =root.getChild("book");
List<Element>book=root.getChildren("book");
for(Element ee:book){
String email =ee.getAttributeValue("email");//属性的读写
System.out.println(email);
ee.setAttribute("email", "li@sina.com");
String name =ee.getChildTextTrim("name");//元素的读写
System.out.println(name);
Element n=ee.getChild("name");
n.setText("goto");
}
XMLOutputter xo =new XMLOutputter();
FileOutputStream fos =new FileOutputStream(file);
xo.output(doc, fos);
fos.close();
}
}
/**lib.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book email="zhang@1.net">
<name>zhang</name>
<price>60.0</price>
</book>
</books>
*/
******************************************************
package ren;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class TestStu {
public static void main(String[] args) throws Exception{
String file ="c:\\student.xml";
SAXBuilder sax= new SAXBuilder();
Document doc=sax.build(file);
Element root=doc.getRootElement();
List<Element> list =root.getChildren("student");//取子节点 要有参数
for(Element ee:list){
String name =ee.getChildTextTrim("name");
System.out.println(name);
String sex =ee.getChildTextTrim("sex");
System.out.println(sex);
List<Element> list2 =ee.getChildren("lesson");
for(Element ee2:list2){
String lessonName =ee2.getChildTextTrim("lessonName");
System.out.println(lessonName);
String lessonScore =ee2.getChildTextTrim("lessonScore");
System.out.println(lessonScore);
}
}
}
}
/**student.xml
* <?xml version="1.0" encoding="GB2312"?>
<StudentInfo>
<student>
<name>达内</name>
<sex>男</sex>
<lesson>
<lessonName>Java初级</lessonName>
<lessonScore>85</lessonScore>
</lesson>
<lesson>
<lessonName>Oracle数据库</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>Java中级</lessonName>
<lessonScore>80</lessonScore>
</lesson>
<lesson>
<lessonName>Java高级</lessonName>
<lessonScore>100</lessonScore>
</lesson>
</student>
<breakLine/>
<student>
<name>花木兰</name>
<sex>女</sex>
<lesson>
<lessonName>射箭</lessonName>
<lessonScore>100</lessonScore>
</lesson>
<lesson>
<lessonName>柔道</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>国学</lessonName>
<lessonScore>80</lessonScore>
</lesson>
<lesson>
<lessonName>烹饪</lessonName>
<lessonScore>60</lessonScore>
</lesson>
<lesson>
<lessonName>游泳</lessonName>
<lessonScore>70</lessonScore>
</lesson>
</student>
<breakLine/>
</StudentInfo>
*/
************************************************
package ren;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class TestHibernate {
public static void main(String[] args) throws Exception{
String file ="c:\\hibernate.cfg.xml";
SAXBuilder sax= new SAXBuilder();
Document doc=sax.build(file);
Element root=doc.getRootElement();
Element session=root.getChild("session-factory");
List<Element> list =session.getChildren("property");
for(Element ee:list){
String name =ee.getAttributeValue("name");
String value =ee.getText();//注意
System.out.println(name+": "+value);
}
List<Element> list2 =session.getChildren("mapping");
for(Element ee2:list2){
String re =ee2.getAttributeValue("resource");
System.out.println(re);
}
}
}
/**
* hibernate.cfg.xml
* <?xml version="1.0" encoding="UTF-8" ?>
- <!-- Generated by MyEclipse Hibernate Tools.
-->
- <hibernate-configuration>
- <session-factory>
<property name="connection.username">hd0701</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.206:1521:tarena</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">connora</property>
<property name="connection.password">hd0701</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<mapping resource="Room.hbm.xml" />
<mapping resource="Users.hbm.xml" />
</session-factory>
</hibernate-configuration>*/