简体   繁体   English

使用SAX Parser的Android XML

[英]Android XML using SAX Parser

<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. 这是我的XML文件,我已经检索了该XML文件中的所有ID值。 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. 但是我想根据名称检索特定的Id值。 For Example, I want to retrieve the ID value 2298 with the help of Home name here 例如,我想在这里的“ 家庭名称”的帮助下检索ID值2298

Below is full parsing Code.. You will get all ID from listMenuId and all name from listMenuName 以下是完全解析代码..你会得到所有ID listMenuId并从所有名称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);
            }
    }
    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM