简体   繁体   中英

Read the Properties in hibernate Configuration File

For Some reason I want to be able to read the Properties in my hibernate Configuration file, say for example I need to know what dialect is used or what database driver class. I have attempted to do this by parsing the xml But I am not getting past the attributes:

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());

            }
        }
    }
}

My Output is:

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

Current Element :property
Driver : hibernate.dialect
null

I need to be able to get the property values any time in my application. Is there another way or what am I missing in this method?

can you try with this

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

Like this, you can get all the properties, set the same somewhere in your static hashMap and use throughout your application.

You can use SAXReader to parse the file, here is the code:

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM