XML: How to read element value?

O

O.B.

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")) {
}
}
}
}
 
M

Martin Honnen

O.B. wrote:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top