getting nodes from XML in java

52 Views Asked by At

I have an XML feed. The XML Nodes are stored in items. There are about 100 Items, And within each Items folder there are several nodes like below:

A
B
C
D
E
F

Node A contains a nodevalue String which can either be SN, REL, KEL and it is this that determines where the Items list belongs in a table.

My aim is that I only want to get B, C, D, E, F nodevalues if node A has a nodevalue String of which is SN. MyComposite2 is a class which contains all the string values from the XML, This is what I have so far:

private MyComposite1R parseItemR(Element item)
{
    MyComposite1R myComposite2=new MyComposite1R();
    NodeList childNodes=item.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++)
    {
        // Processing some element in the form: <name>value</name>
        Element element = (Element)childNodes.item(i);
        String nodeName = element.getNodeName();

        String nodeValue = element.getTextContent();

        // Decide where to store the node value (in myComposite) depending on the node name:
        switch (nodeName)
        {
            case "A":

            myComposite2.setA(nodeValue);


            break;
            case "B":
              myComposite2.setB(nodeValue); 
                break;
            case "C":
              myComposite2.setC(nodeValue); 
                break;
                case "D":
              myComposite2.setD(nodeValue); 
                break;

                  case "E":
              myComposite2.setE(nodeValue); 
                break;

                    case "F":
              myComposite2.setF(nodeValue); 
                break;   

            default: // An unknown node was found.
                System.err.println("Warning: Node '"+nodeName+"' not recognized and will be ignored");
        }
    }


    return myComposite2;
}
0

There are 0 best solutions below