Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

XML: How to read element value?

Thread view: 
O.B. - 29 Jan 2006 04:16 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.