Posted on 2008-11-13
Last Modified: 2013-11-24
I am using KXML parser where  everything works, but I would like to try and get a list of node names.  I would like to return child_one, child_two from the following xml:

<?xml version="1.0"?>
   <child name="child_one">Hello</child>
   <child name="child_two">World</child>

In servlets I have used the following imports and code to get the node names:

import javax.xml.parsers.*;
import org.w3c.dom.*;

int count=0;
      for (int i=0 ; i<nodes.getLength() ; i++) {
                                  Node node = nodes.item(i);
                    if (node instanceof Element) {
            String nodeName = node.getNodeName();
            String myData =  node.getTextContent();
            System.out.println("Node: " + nodeName + " has data " + myData);

Question by:vandy02
    LVL 10

    Accepted Solution


      if (node instanceof Element) {
     Element e = (Element) node ;
    String n = e.getAttributeValue (0) ;
    //n is the name attribute in Child

    Open in new window

    LVL 10

    Expert Comment

    if you want to get all the attributes, you would need to loop over the attributes or get the attribute with namespace & name,
    LVL 86

    Expert Comment

    You would do something like the following:
                            switch (event) {
                            case XMLStreamConstants.START_ELEMENT:
                                int numAttrs = parser.getAttributeCount();
                                if (numAttrs > 0) {
                                    for(int i = 0;i < numAttrs;i++) {
                                        System.out.printf("Attribute %s=%s\n", parser.getAttributeName(i), parser.getAttributeValue(i));
                                System.out.printf("Name = %s\n", parser.getName());

    Open in new window


    Author Comment

    I am going to try this tonight.  Sorry, I have not been able to try it since it was posted.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now