Search code examples
androidxmlmenulistenersubmenu

Get all data and sub data of a complexe xml in organized way


I want to get data in my xml with her associated sub data : for example I want to take all the name3 in categorie2 and all the name5 who are in this name3 and all the name7 who are in this name5 etc...

Currently I only know how to get all the name3, all the name 5 and all the name7 but I can't know who associated with who.

If you need more information, ask me.

Thank for your help.

EDIT

I tried this for take data :

 url3= new String("");
                    xml3 = parser.getXmlFromUrl(url3);
                    Document doc3 = parser.getDomElement(xml3);

                    //System.out.println(xml);

                    menuBase = new ArrayList<HashMap<String, String>>();

                    NodeList nodes = doc3.getElementsByTagName(KEY_CATEGORY_2);
                    for (int i = 0; i < nodes.getLength(); i++) {

                        HashMap<String, String> map = new HashMap<String, String>();
                        Element element = (Element) nodes.item(i);
                        NodeList title = element.getElementsByTagName(KEY_NAME_3);
                        Element line = (Element) title.item(0);

                        map.put(KEY_NAME_3, getCharacterDataFromElement(line));

                        menuBase.add(map);
                        menu1 = new ArrayList<ArrayList<HashMap<String, String>>>();

                        ArrayList<HashMap<String, String>> map1 = new ArrayList<HashMap<String, String>>();
                        NodeList n = doc3.getElementsByTagName(KEY_CATEGORY_4);
                        for (int y = 0; y < n.getLength(); y++) {
                            HashMap<String, String> map1bis = new HashMap<String, String>();
                            Element e = (Element) n.item(y);
                            NodeList title1 = e.getElementsByTagName(KEY_NAME_5);
                            Element line1 = (Element) title1.item(0);

                            map1bis.put(KEY_NAME_5, getCharacterDataFromElement(line1));

                            map1.add(map1bis);


                            menu1.add(map1);

                        }   

                    }

and then for i tried to create menu and sub menu like that:

for(int i=0;i<menuBase.size();i++){
    SubMenu x=menu.addSubMenu(menuBase.get(i).get(KEY_NAME_3).toString());
    for(int y=0;y<menu1.get(i).size();y++){
        SubMenu w=x.addSubMenu(menu1.get(i).get(y).get(KEY_NAME_5).toString());
    }
}

but in each menu item i have all my sub menu item


Solution

  • I finally got a solution of mine probleme :

    NodeList nodes = doc3.getElementsByTagName(KEY_CATEGORY_2);
            int sum0=0;      
            for(int i=0;i<menuBase.size();i++){
                SubMenu x=menu.addSubMenu(changeToRegularForm(menuBase.get(i).get(KEY_NAME_3).toString()));
                    int count0 = ((Element)(nodes.item(i))).getElementsByTagName(KEY_CATEGORY_4).getLength();
                    System.out.println(count0);
                    if(count0==0){
                        /*x.findItem(i).set*/
                    }else{
                        for(int y=sum0;y<sum0+count0;y++){
                        SubMenu w=x.addSubMenu(changeToRegularForm(menu1.get(y).get(KEY_NAME_5).toString()));
                    }
    
                    }
                    sum0+=count0;
            }
    

    explanation : Finally i take all the child element in a one arryList but i use a fonction to count how each element have child element and had them.

    If you need more information/accuracy, just ask for.