Given the following XML file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="MySessionFactory">
<property name="connection.url">jdbc:postgresql:test</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
</session-factory>
</hibernate-configuration>
I can't seem to figure out how to parse out the connection.url value of
"jdbc:postgresql:test". Below is my code:
Document hibernateDoc = ParserAdapter.parseXmlFile(inputStream, false);
//
// Get a list of "property" elements
//
NodeList nodeList = hibernateDoc.getElementsByTagName("property");
for (int i=0; i<nodeList.getLength(); i++) {
Node node = nodeList.item(i);
//
// Verify that the property is of "session-factory"
//
if (0 ==
node.getParentNode().getNodeName().compareToIgnoreCase("session-factory")) {
Node attribute = node.getAttributes().getNamedItem("name");
if ( null != attribute) {
if (0 ==
attribute.getNodeValue().compareToIgnoreCase("connection.url")) {
//
// What now?
//
} else if (0 ==
attribute.getNodeValue().compareToIgnoreCase("connection.username")) {
} else if (0 ==
attribute.getNodeValue().compareToIgnoreCase("connection.password")) {
}
}
}
}
George - 29 Jan 2006 05:09 GMT
In your "what now" block, try:
String value = node.getFirstChild().getNodeValue();
> Given the following XML file:
>
[quoted text clipped - 41 lines]
> }
> }
Martin Honnen - 29 Jan 2006 13:34 GMT
> <property name="connection.url">jdbc:postgresql:test</property>
> NodeList nodeList = hibernateDoc.getElementsByTagName("property");
> for (int i=0; i<nodeList.getLength(); i++) {
> Node node = nodeList.item(i);
Element in terms of the W3C DOM have a node value of null, if you have a
W3C DOM Level 3 Core (Sun Java JDK 1.5 has that, 1.4 not) then you can
access
node.getTextContent()
however to find out the contents of elements node.
Otherwise, as already suggested, in that case above accessing the text
child node
node.getFirstChild()
and then its node value should do
node.getFirstChild().getNodeValue()

Signature
Martin Honnen
http://JavaScript.FAQTs.com/