XML : Fetch only unique values from XML DOM parser android

Want to fetch only the unique values from the XML which i am getting in my spinner.... i am able to get the values in my spinner but all dublicate values are also showing in spinner dropdown.

For example my xml is

  <a:AAAA>  <a:Q>123</a:Q>  <a:W>yessssssss</a:W>   <--- THIS VALUE IS SAME   <a:E>275</a:E>                   ^  <a:R>wwwwqwq</a:R>               |  </a:AAAA>               So want only single value in andrid spinner how to achive this?                                                                 <a:AAAA>                         |  <a:Q>456</a:Q>                   v  <a:W>yessssssss</a:W>   <--- AND THIS VALUE IS SAME   <a:E>648</a:E>  <a:R>qwqwqsd</a:R>  </a:AAAA>     <a:AAAA>  <a:Q>789</a:Q>  <a:W>Hiiii</a:W>  <a:E>269</a:E>  <a:R>ds</a:R>  </a:AAAA>    <a:AAAA>  <a:Q>867</a:Q>  <a:W>qwqwqw</a:W>  <a:E>1648</a:E>  <a:R>wqw</a:R>  </a:AAAA>    

below code is showing the value in spinner but with duplicate data

    button.setOnClickListener(new OnClickListener()          {                @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                    parse();                    from_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title);                  from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                  spinner.setAdapter(from_adapter);                    }            });          }        public void onItemSelected(AdapterView<?> parent, View view, int pos,              long id) {          Toast.makeText(parent.getContext(), ""+spinner.getSelectedItem().toString().trim(),                  Toast.LENGTH_LONG).show();      }        public void onNothingSelected(AdapterView<?> arg0) {      }        protected void parse() {          // TODO Auto-generated method stub              try {                URL url = new URL(                      "http://xyz.xml");              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();              DocumentBuilder db = dbf.newDocumentBuilder();              Document doc = db.parse(new InputSource(url.openStream()));              doc.getDocumentElement().normalize();                NodeList nodeList = doc.getElementsByTagName("a:AAAA");              for (int i = 0; i < nodeList.getLength(); i++) {                    Node node = nodeList.item(i);                           Element fstElmnt = (Element) node;                  NodeList nameList = fstElmnt.getElementsByTagName("a:Q");                  Element nameElement = (Element) nameList.item(0);                  nameList = nameElement.getChildNodes();                               NodeList websiteList = fstElmnt.getElementsByTagName("a:W");                  Element websiteElement = (Element) websiteList.item(0);                  websiteList = websiteElement.getChildNodes();                    NodeList websiteList1 = fstElmnt.getElementsByTagName("a:E");                  Element websiteElement1 = (Element) websiteList1.item(0);                  websiteList1 = websiteElement1.getChildNodes();                    NodeList websiteList2 = fstElmnt.getElementsByTagName("a:R");                  Element websiteElement2 = (Element) websiteList2.item(0);                  websiteList2 = websiteElement2.getChildNodes();                      title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());                }    

No comments:

Post a Comment