分类: 系统运维
2010-09-26 00:04:24
dom4j download
用myeclipse创建工程Prj_XML,导入dom4j-1.6.1.jar文件
1.定义一个xml文档User.hbm.xml
public class DOM4JTest {
public static void main(String[] args) {
//1.从文件读取XML
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(new File("User.hbm.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
//2.取得root节点
Element rootElement = document.getRootElement();
p(rootElement.getName());
//3.遍历XML树
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());
}
}
}
public static void p(Object o) {
System.out.println(o);
}
}
3.运行结果
hibernate-mapping
class
name-------com.chao.User
table-------t_user