阅读休眠配置文件中的属性

时间:2014-09-01 04:17:27

标签: java hibernate xml-parsing

由于某些原因,我希望能够读取我的hibernate配置文件中的属性,比如说我需要知道使用什么方言或什么数据库驱动程序类。我试图通过解析xml来做到这一点但是我没有超越属性:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author User
 */
public class XmlParser {

    public XmlParser() {
    }

    public void readXml() throws SAXException, IOException, ParserConfigurationException {
        InputStream in = this.getClass().getResourceAsStream("/hibernate.cfg.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(in);
        doc.getDocumentElement().normalize();
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("property");
        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("Driver : " + eElement.getAttribute("name"));
                System.out.println("Not Sure : " + eElement.getElementsByTagName("hibernate.connection.url").item(0).getTextContent());
                System.out.println(eElement.getElementsByTagNameNS("name", "hibernate.dialect").item(0).getTextContent());

            }
        }
    }
}

我的输出是:

Root element :hibernate-configuration
----------------------------

Current Element :property
Driver : hibernate.dialect
null

我需要能够在我的应用程序中随时获取属性值。还有其他方法或我在这种方法中缺少什么?

2 个答案:

答案 0 :(得分:1)

你可以试试这个

吗?
Configuration hibernateConfiguration = new Configuration().configure(new File("/hibernate.cfg.xml"));
String url = configuration.getProperty("hibernate.connection.url");

像这样,您可以获取所有属性,在静态hashMap中的某处设置相同的属性,并在整个应用程序中使用。

答案 1 :(得分:0)

您可以使用SAXReader来解析文件,这里是代码:

import java.io.InputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXMLFile {

    public static void main(String argv[]) throws DocumentException {
        new ReadXMLFile().readMyXML();
    }

    private void readMyXML() throws DocumentException {
        InputStream in = this.getClass().getResourceAsStream(
                "/hibernate.cfg.xml");
        Document document = new SAXReader().read(in);
        Element root = document.getRootElement();
        System.out.println("Root element : " + root.getName());
        Element sfNode = document.getRootElement().element("session-factory");
        Iterator<Element> itr = sfNode.elementIterator("property");
        System.out.println("----------Properties-----------");
        while (itr.hasNext()) {
            Element node = itr.next();
            String name = node.attributeValue("name");
            String value = node.getText().trim();
            System.out.println(name + " = " + value);
        }
    }
}