0
<MenuByRole InstanceID="519" RoleID="614">
    <MainMenu Id="2298">Home</MainMenu>
    <MainMenu Id="2431">My Results</MainMenu>
    <MainMenu Id="2415">My Attendance</MainMenu>
</MenuByRole>

This is my XML file, I have retrieved all the ID values in this XML file. Here is the following code for that,

if (qName.equalsIgnoreCase("MainMenu"))
        {
      id = attributes.getValue("Id");
      MainMenu = true;
    }

But I want to retrieve the specific Id value according to the name. For Example, I want to retrieve the ID value 2298 with the help of Home name here

1
  • I have displayed the names like Home,My Results,My Attendace in Listview when I click on the List item I have to get its Id value. For example, When I clicked the Home List Item ,I have to get the 2298 as result Commented Dec 5, 2013 at 7:11

2 Answers 2

1

Below is full parsing Code.. You will get all ID from listMenuId and all name from listMenuName

String currentValue = "";
String currentTag = "";
        ArrayList<String> listMenuId;
        ArrayList<String> listMenuName;

        // Called when tag starts
        @Override
        public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
            currentTag = localName;
            if(currentTag.equalsIgnoreCase("MenuByRole")){
                listMenuId = new ArrayList<String>();
                listMenuName = new ArrayList<String>();
            }
            if(currentTag.equalsIgnoreCase("MainMenu")){
                listMenuId.add(attributes.getValue("Id"));
            }
        }

        // Called when tag closing
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
        }

        // Called to get tag characters
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            currentValue = currentValue + new String(ch, start, length);

            if(currentTag.equalsIgnoreCase("MainMenu"))
                listMenuId.add(currentValue);
            }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

You can use HashMap along with this solution... for key value pairs :)
Updated answer, check now
Here I am displaying the Home,My Results,My Attendance in a ListView when I click on the List item I want to retrieve its Id value
0
    public class MainMenu {
            private Long id;
            private String Name;

            public Long getId() {
                return id;
            }

            public void setId(Long id) {
                this.id = id;
            }

            public String getName() {
                return Name;
            }

            public void setName(String name) {
                Name = name;
            }

        }

    ArrayList<MainMenu> munuIDList;
        MainMenu oMainMenu;
        boolean isReadData;
        String sName;
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if(localName.equalsIgnoreCase("MenuByRole")){
                munuIDList = new ArrayList<MainMenu>();
                isReadData = false;
            }
            if(localName.equalsIgnoreCase("MainMenu")){
                isReadData = true;
                oMainMenu = new MainMenu();
                oMainMenu.setId(attributes.getValue("Id"));

            }
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if(localName.equalsIgnoreCase("MainMenu"))
                oMainMenu.setName(sName);
            sName = null;
            isReadData = false;
            }
        }
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if(isReadData) {
            sName = new String(ch, start, length);
            }
    }

MainActivity
public class MainActivity extends Activity implements OnItemClickListener{
ArrayLsit<MainMenu> lstMainMenu;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                myListView = (ListView) findViewById(R.id.myListView);
               myListView.setOnItemClickListener(this);
}
}

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, lstMainMenu.get(arg2).getId(), Toast.LENGTH_SHORT).show();
    }

4 Comments

I have displayed the names like Home,My Results,My Attendace in Listview when I click on the List item I have to get its Id value. For example, When I clicked the Home List Item ,I have to get the 2298 as result
Update me with logCat records
FATAL EXCEPTION: main java.lang.NullPointerException at com.javatpoint.saxxmlparsing.MainActivity.onItemClick(MainActivity.java:106) at android.widget.AdapterView.performItemClick(AdapterView.java:284) at android.widget.ListView.performItemClick(ListView.java:3382) at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) at android.os.Handler.handleCallback(Handler.java:587) 12-05 14:40:53.280:
I have sent a mail to your email address please check it once is that I have sended to the right email address or not

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.