Jdom生成hibernate.cfg.xml和读取其中内容

package com.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.Comment;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Create {
	
	public static void main(String[] args) {
		//xml文档
		Document document = new Document();
		//DocType声明
		DocType docType = new DocType("hibernate-configuration" , "-//Hibernate/Hibernate Configuration DTD 3.0//EN" , "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
		//使用doc声明
		document.setDocType(docType);
		//根结点 hibernate-configuration
		Element root = new Element("hibernate-configuration");
		//设置document的根结点
		document.setRootElement(root);
		//节点 session-factory
		Element sessionfactory = new Element("session-factory");
		//将session-factory设置到root
		root.addContent(sessionfactory);
		//添加注释
		sessionfactory.addContent(new Comment("Database connection settings"));
		//添加driver-class
		Element driverClass = new Element("property");
		driverClass.setAttribute("name", "connection.driver_class");
		driverClass.setText("com.mysql.jdbc.Driver");
		sessionfactory.addContent(driverClass);
		//添加url
		Element url = new Element("property");
		url.setAttribute("name", "connection.url");
		url.setText("jdbc:mysql://localhost:3306/1ting");
		sessionfactory.addContent(url);
		//添加用户名
		Element username = new Element("property");
		username.setAttribute("name", "connection.username");
		username.setText("root");
		sessionfactory.addContent(username);
		//添加密码
		Element password = new Element("property");
		password.setAttribute("name", "connection.username");
		password.setText("root");
		sessionfactory.addContent(password);
		//添加注释
		sessionfactory.addContent(new Comment("JDBC connection pool (use the built-in)"));
		//添加pool
		Element pool = new Element("property");
		pool.setAttribute("name", "connection.pool_size");
		pool.setText("1");
		sessionfactory.addContent(pool);
		//添加注释
		sessionfactory.addContent(new Comment("SQL dialect"));
		//添加方言
		Element dialect = new Element("property");
		dialect.setAttribute("name", "dialect");
		dialect.setText("org.hibernate.dialect.MySQL5Dialect");
		sessionfactory.addContent(dialect);
		//添加注释
		sessionfactory.addContent(new Comment("Echo all executed SQL to stdout"));
		//添加showsql
		Element showsql = new Element("property");
		showsql.setAttribute("name", "show_sql");
		showsql.setText("true");
		sessionfactory.addContent(showsql);
		//添加注释
		sessionfactory.addContent(new Comment("Drop and re-create the database schema on startup"));
		//添加hbm2ddl
		Element hbm2ddl = new Element("property");
		hbm2ddl.setAttribute("name", "hbm2ddl.auto");
		hbm2ddl.setText("update");
		sessionfactory.addContent(hbm2ddl);
		//添加注释
		sessionfactory.addContent(new Comment("mapping class"));
		//添加mapping
		Element mapping1 = new Element("mapping");
		mapping1.setAttribute("class", "edu.hzu.entity.SingerType");
		sessionfactory.addContent(mapping1);
		
		Element mapping2 = new Element("mapping");
		mapping2.setAttribute("class", "edu.hzu.entity.Singer");
		sessionfactory.addContent(mapping2);
		//生成xml
		XMLOutputter outputter = new XMLOutputter();
		//获得漂亮输出
		Format format = Format.getPrettyFormat();
		outputter.setFormat(format);
		//生成xml文件
		try {
			outputter.output(document, new FileOutputStream("hibernate.cfg.xml"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

package com.test;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class Read {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		SAXBuilder builder = new SAXBuilder();
		try {
			System.out.println("Get all properties");
			System.out.println("----------------------");
			Document document = builder.build(new File("hibernate.cfg.xml"));
			//首先获得所有的property节点的name和text
			List<Element> properties = XPath.selectNodes(document, "/hibernate-configuration/session-factory/property");
			for (Element property : properties) {
				System.out.println(property.getAttributeValue("name") + " : " + property.getText());
			}
			System.out.println("----------------------");
			
			System.out.println("Get all Mappings");
			System.out.println("----------------------");
			//再次获得所有mapping节点的class
			List<Element> mappings = XPath.selectNodes(document, "/hibernate-configuration/session-factory/mapping");
			for (Element mapping : mappings) {
				System.out.println(mapping.getAttributeValue("class"));
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值