KXML PARSER -- GETTING LIST OF NODES

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"?>
<demo>
   <child name="child_one">Hello</child>
   <child name="child_two">World</child>
</demo>

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

import java.io.*;
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);
..........

Thanks
vandy02Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mukundha_expertCommented:

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mukundha_expertCommented:
if you want to get all the attributes, you would need to loop over the attributes or get the attribute with namespace & name,
http://kxml.sourceforge.net/kxml2/javadoc/
CEHJCommented:
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());
                                break;

Open in new window

vandy02Author Commented:
I am going to try this tonight.  Sorry, I have not been able to try it since it was posted.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.