肥皂响应解析

时间:2013-08-05 04:43:32

标签: android

我从服务器获得响应,我想解析它并添加到arraylist但我得到Classcastexception。请帮我解析一下。

<NewDataSet> 
<Table> 
<Mother_Name>swati</Mother_Name> 
<Mother_DOB>15/01/1987</Mother_DOB> 
<Mother_Age>30</Mother_Age> 
<Mother_ExpConceiveDate>28/07/2013</Mother_ExpConceiveDate> 
<Alert_Description>mother alert</Alert_Description> 
</Table> 
</NewDataSet>

这是我的代码

                            int count = soapResponse.getPropertyCount();                
            SoapObject response1 = (SoapObject) response;
            Log.w("response",""+response1.getProperty(0));

            for (int j = 0; j < count; j++) {
                /** Temp SeatInfo soap object */
                SoapObject soChild = (SoapObject) response1.getProperty(j);

                Log.w("motherName",""+soChild.getProperty(0));
                    }

1 个答案:

答案 0 :(得分:2)

使用xml pullparser。这将返回xml的数组列表。

MyXmlPullParser objMyXmlPullParser = new MyXmlPullParser(context);
                        List<Map<String , String>> list = objMyXmlPullParser.readXml("Xml respose put here", "Table");



public class MyXmlPullParser 
{
    Context _context ;

    public MyXmlPullParser(Context _context)
    {
        this._context = _context ;
    }

    public List<Map<String , String>> readXml(String XmlString , String ParentTag)
    {
        Map<String , String > map = new HashMap<String, String>();
        List<Map<String , String >> list = new ArrayList<Map<String , String >>();

        try 
        {
            String Tag = "" ;
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader (XmlString));
            int eventType = xpp.getEventType();

            while (true) 
            {
                if(eventType == XmlPullParser.START_TAG) 
                {
                    Tag = xpp.getName();
                } 
                else if(eventType == XmlPullParser.END_TAG) 
                {
                    Tag = "" ;
                    if(xpp.getName().equals(ParentTag))
                    {   
                        list.add(map);
                        map = new HashMap<String, String>();
                    }
                }
                else if(eventType == XmlPullParser.TEXT) 
                {
                    String text = xpp.getText();
                    if(!Tag.equals("") && !Tag.equals(ParentTag)) 
                    {
                        map.put(Tag, text);
                    }
                }
                else if(eventType == XmlPullParser.END_DOCUMENT) 
                {
                    System.out.println("End document");
                    break ;
                }
                eventType = xpp.next();
            }
        }

        catch (XmlPullParserException e) 
        {
            Log.e("xml reader" , "error in parsing  xml");
            return null ;
        }
        catch (IOException e)  
        {
            Log.e("xml reader" , "error in IO in xml");
            return null ;
        }
        return list ;
    }