• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 976
  • Last Modified:

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
0
vandy02
Asked:
vandy02
  • 2
1 Solution
 
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

0
 
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/
0
 
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

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now