0

I am trying to parse an xml but it is returning all the chld nodes that have tag "nature".But I just want to only those nodes whose cat is selcted.

My xml looks like :-

<NatureList>
  <NatureCategory cat="Crimes Against People">
<Nature>
<Name>Aggravated Assault</Name>
</Nature>
<Nature>
<Name>Annoyance Phone Calls</Name>
</Nature>
<NatureCategory>
<NatureCategory ...NatureCategory>

and parsing is done like this :

Document doc = XmlParser.getDomElement(xml);
    NodeList n = doc.getElementsByTagName("NatureCategory");

        try {


        for(int i=0; i<n.getLength();i++)
        {
            Element e = (Element) n.item(i);

            if(e.getAttribute("cat").equals(spinn.getSelectedItem().toString())) 
            {
                Log.i("sub1", spinn.getSelectedItem().toString()  );
                NodeList n1 = doc.getElementsByTagName("Nature");
                for(int j=0; j<n1.getLength();j++)
                {
                    Element e1 = (Element) n1.item(j);
                    subcategory.add(getValue(e1, "Name"));


                }
            }
        //  subcategory.add()

        }
        }
7
  • cat is selcted in what like in a spinner u means like if spinner select string is Crimes Against People then only that related nature will be parse Commented Apr 4, 2014 at 9:35
  • then whats the prob like u dont able to parse the same name child tag ?? Commented Apr 4, 2014 at 9:37
  • I just want to parse the child whose cat is selected .For ex, if Crimes Against People is selected then all the child under this should be loaded Commented Apr 4, 2014 at 9:39
  • ohk see my answer u get that string which will be select in spinner then just compare with NatureCategory and then i write all code for child parsing Commented Apr 4, 2014 at 9:42
  • sure dear but not copy paste u also use your own logic ........... Commented Apr 4, 2014 at 9:44

2 Answers 2

1
nodes1 = doc.getElementsByTagName("NatureList");                  

    for (int i = 0; i < nodes1.getLength(); i++) {

        ObjectClass cgro = new ObjectClass();
        Element e = (Element)nodes1.item(i);
        cgro.NatureCategory = XMLfunctions.getValue(e, "NatureCategory");//here u can compare with your spinner string if match then parse this NatureCategory or save oe any logic which u like 


        nodes1a = doc.getElementsByTagName("cat");

        for(int j = 0; j < nodes1a.getLength(); j++ ){

            ObjectClass1 cgro1 = new ObjectClass1();

             Element e2= (Element) nodes1a.item(j);
             cgro1.cat= XMLfunctions.getCharacterDataFromElement(e2);
             ArrayListClass.ItemList1.add(cgro1);
        }


        ArrayListClass.ItemList2.add(cgro);

        }

and class use for this

public class XMLfunctions {

    public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

    DocumentBuilder db = dbf.newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
    System.out.println("XML parse error: " + e.getMessage());
    return null;
    } catch (SAXException e) {
    System.out.println("Wrong XML file structure: " + e.getMessage());
    return null;
    } catch (IOException e) {
    System.out.println("I/O exeption: " + e.getMessage());
    return null;
    }

    return doc;

    }

    /** Returns element value
    * @param elem element (it is XML tag)
    * @return Element value otherwise empty String
    */
Sign up to request clarification or add additional context in comments.

Comments

0

I think if form your XML like this it would be better:

<NatureList>
  <NatureCategory>     
      <cat>Crimes Against People</cat>    
      <Nature>
        <Name>Aggravated Assault</Name>
    </Nature>

    <Nature>
         <Name>Annoyance Phone Calls</Name>

    </Nature>
    <NatureCategory>

Then get the cat tag and then test it.

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.