从xml文件解析并根据标记的内容进行分组

时间:2012-10-03 11:24:01

标签: android xml-parsing

这是我的xml文件 http://www.yupptv.com/mobile/androidxml.aspx 我想根据类型显示名称,ID,图像,类型和链接的频道 即如果type是'news channel',我必须显示该类型下的所有频道。 我怎样才能做到这一点? 这是我的listactivity代码 `

    ArrayList<HashMap<String, String>> channelist = new ArrayList<HashMap<String, String>>();
    XMLParser parser = new XMLParser();

    String xml = parser.getXmlFromUrl(url);// getting XML

    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(CHANNEL);

    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) 
    {

        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => valuez
        map.put(NAME, parser.getValue(e,NAME ));
        Log.i("name"+ NAME, "created");
        map.put(ID, parser.getValue(e,ID));
        map.put(TYPE,parser.getValue(e,TYPE ));
        map.put(LANGUAGE,parser.getValue(e,LANGUAGE ));
        map.put(IMAGE,parser.getValue(e,IMAGE));
        map.put(LINK,parser.getValue(e,LINK ));
        map.put(BANDWIDTH,parser.getValue(e,BANDWIDTH));
        map.put(CELLNAPID, parser.getValue(e,CELLNAPID ));

              if(map.containsKey(TYPE)&&TYPE=="News Channel")
     {
         channelist.add(map);     
     }        
    }

        ListAdapter adapter = new SimpleAdapter(this, channelist,
        R.layout.list_item,
        new String[] { NAME, ID,TYPE,LANGUAGE,IMAGE,LINK,BANDWIDTH,CELLNAPID}, new int[] {
                R.id.name, R.id.id,R.id.type,R.id.language,R.id.image,R.id.link,R.id.bandwidth,R.id.cellnapid });

setListAdapter(adapter);

`

xml parser.java

public String getXmlFromUrl(String url) {
    String xml =null ;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);



    }
    catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    } 
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
       // e.printStackTrace(err);
    }
    // return XML

    return xml;

}


public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();


            doc = db.parse("http://www.yupptv.com/mobile/androidxml.aspx");


        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } 
    catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

            // return DOM
        return doc;

}
public String getValue(Element item, String str) 
{
    NodeList n = item.getElementsByTagName(str);

    return this.getElementValue(n.item(0));

}

public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";

} 

请帮忙

提前致谢

0 个答案:

没有答案