Results 1 to 10 of 10
  1. #1
    rsenth99 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Question How to get a node value of an XML element?

    Hi all

    I am trying to parse an xml file and get the node value.

    XML Code:
    <assign name="Transform_1">
    <bpelx:annotation>
    <bpelx: pattern>transformation</bpelx: pattern>
    </bpelx:annotation>
    </assign>

    I need to get the node value of the element 'bpelx: pattern'.

    Java Code:
    if (list1.getLength() != 0) {
    for (int k = 0; k < list1.getLength(); k++) {
    Node node = list1.item(k);
    System.out.println("Node Name : "+node.getNodeName());
    NamedNodeMap nodeMap = node.getAttributes();
    System.out.println("Node Value : "+node.getNodeValue());

    But I am getting the node value as 'NULL'. Please let me know your valuable suggestions to solve this issue.
    Last edited by rsenth99; 01-19-2010 at 05:29 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What is list1?
    That code really doesn't help us in determine how you're trying to get hold of the node in question.

  3. #3
    rsenth99 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    'list1' is an object that contains all the elements in the XML file.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    OK, clearly you missed the not-so-subtle request for more code...
    So list1 is a NodeList? Gained from a getElementsByTagName (or similar)?

    Anyway...

    Look at the api and see if you can guess why nodeValue is always null. Remember that your NodeList is almost certainly all elements.

  5. #5
    rsenth99 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default Yes

    NodeList list1 = doc.getElementsByTagName("*");

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Why are you selecting every node when you're only interested in the <pattern> ones?
    And you'll want to cast the Nodes in that list to Elements so you can actually use them.

  7. #7
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    Use another parser? :p
    I use stax, its faster and easier to understand
    what does it returns when calling method getLocalName()?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    The parser really doesn't matter. If they're not using the one they have properly then there's really no reason to expect them to use a different one properly.

  9. #9
    ess
    ess is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    getNodeValue() returns null for all Element (see javadoc of org.w3c.dom.Node), so use getTextContent() instead.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

Similar Threads

  1. Get selected Node Value of a child element
    By Ms.Ranjan in forum XML
    Replies: 6
    Last Post: 10-06-2009, 09:31 PM
  2. How to add JCheckbox as a node in JTree
    By shajuantony in forum javax.swing
    Replies: 0
    Last Post: 04-09-2009, 07:19 AM
  3. XML Node.getNodeValue Problem
    By mindscape777 in forum XML
    Replies: 1
    Last Post: 01-11-2009, 02:22 PM
  4. How to disabled a node.
    By smartsubroto in forum New To Java
    Replies: 32
    Last Post: 07-01-2008, 07:30 AM
  5. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11:25 PM

Posting Permissions

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