Results 1 to 2 of 2
  1. #1
    sito42 is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default How to get value of specific child node

    I am trying to access the password of a specific user like jsmith in the following xml:

    <xml>
    <Users>
    <User>
    <Name>jsmith</Name>
    <Password>xyz</Password>
    </User>
    <User>
    <Name>kmiller</Name>
    <Password>abc</Password>
    </Users>
    <xml>

    Java Code:
    Element node = (Element) XPathAPI.selectSingleNode(document, "/Users/User[Username=\"" + username + "\"]");
    if (node != null) {
      Element pswdNode = (Element)XPathAPI.selectSingleNode(node, "Password");
      String value = pwdNode.getFirstChild().getNodeValue();
    }
    The code above gives me a value of null. What am I doing wrong?

    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by sito42 View Post
    I am trying to access the password of a specific user like jsmith in the following xml:

    <xml>
    <Users>
    <User>
    <Name>jsmith</Name>
    <Password>xyz</Password>
    </User>
    <User>
    <Name>kmiller</Name>
    <Password>abc</Password>
    </Users>
    <xml>

    Java Code:
    Element node = (Element) XPathAPI.selectSingleNode(document, "/Users/User[Username=\"" + username + "\"]");
    if (node != null) {
      Element pswdNode = (Element)XPathAPI.selectSingleNode(node, "Password");
      String value = pwdNode.getFirstChild().getNodeValue();
    }
    The code above gives me a value of null. What am I doing wrong?

    Thanks!
    OK, assuming the <xml> bits aren't actually part of your xml (and the lack of closing User tag in the second User is a typo)...you're looking for a Username element, where the element is actually called Name.

Similar Threads

  1. how to get the child class in inheritance?
    By java_fun2007 in forum New To Java
    Replies: 7
    Last Post: 09-29-2010, 09:35 AM
  2. Cannot add or update a child row....
    By Pierced1 in forum JDBC
    Replies: 2
    Last Post: 06-29-2009, 02:32 AM
  3. Child Labor
    By freddieMaize in forum Forum Lobby
    Replies: 1
    Last Post: 11-16-2008, 08:59 PM
  4. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  5. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •