1

I am trying to use a variable that specifies which parent.child nodes I am parsing.

below is my current xml:

<results>
    <GW>
            <result>
                     <item>Car</item>
                     <name>Bob</name
            </result>
            <result>
                     <item>Bike</item>
                     <name>Tom</name
            </result>
    </GW>

    <BF>
            <result>
                     <item>Apple</item>
                     <name>Mike</name
            </result>
            <result>
                     <item>Melon</item>
                     <name>Julia</name
            </result>
    </BF>


</results>

And here is my parsing code. I want to use the variable items to tell which node I am supposed to parse GW or BF

//DOC IS ASSIGNED THE XML DATA EARLIER IN THE CODE

 Bundle bundle = getIntent().getExtras();
 int ITEMS = bundle.getInt("selection");


NodeList nodes = doc.node[ITEMS].getElementsByTagName("result");

for (int i = 0; i < nodes.getLength(); i++) {
    HashMap<String, String> map = new HashMap<String, String>();    

    Element e = (Element)nodes.item(i);
    map.put("main_content", XMLfunctions.getValue(e, "item"));
    map.put("name", XMLfunctions.getValue(e, "name"));
    mylist.add(map);            
}

I am trying to only parse either the child nodes of GW or BF and that depends on the value of ITEMS. So if items is equal to 0 then I would get the data from GW and if it is 1 I would get the data from BF.

If I could guess it would be something like:

NodeList nodes = doc.childNode[ITEMS].getElementsByTagName("result");
2
  • Could you explain what you want to get done? Commented Jun 22, 2011 at 3:35
  • I updated my question above. Please let me know if it is not clear. Commented Jun 22, 2011 at 3:43

1 Answer 1

1
Element docElem = doc.getDocumentElement();
NodeList nl = docElem.getElementsByTagName("results");
Element elem = (Element)nl.item(ITEMS);
nodes = elem.getElementsByTagName("result");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.