Search code examples
androidxml-parsingksoap2android-ksoap2

Ksoap2 parser xml data


I have used Ksoap2 to connect to this .NET web service and i get a xml response when i enter the users id. I only want to see two tags callTitle and callDescription. I don't need the rest and want to see in text not surround with xml code. can some one please help? I can't find tutorial online.

public class AndroidWebService extends Activity {

    /** Called when the activity is first created. */
    private static String SOAP_ACTION = "http://tempuri.org/GetHelpDeskCalls";
    private static String NAMESPACE = "http://tempuri.org/";
    private static String METHOD_NAME = "GetHelpDeskCalls";
    static final String URL = "https:/192.2344.123:8080/Service1.asmx";

    Button getData;
    EditText userID;
    TextView data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helpdesk);

        getData = (Button) findViewById(R.id.button1);
        userID = (EditText) findViewById(R.id.txtFar);
        data = (TextView) findViewById(R.id.textView1);

        Thread nT = new Thread() {
            @Override
            public void run() {

                getData.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        SoapObject request = new SoapObject(NAMESPACE,
                                METHOD_NAME);

                        request.addProperty("userID", userID.getText()
                                .toString());




                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                SoapEnvelope.VER11);

                        envelope.setOutputSoapObject(request);
                        envelope.dotNet = true;
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(
                                URL);

                        try {

                            androidHttpTransport.debug = true;
                            androidHttpTransport.call(SOAP_ACTION, envelope);

                            final String results = androidHttpTransport.responseDump
                                    .toString();

                            runOnUiThread(new Runnable() {
                                public void run() {

                                    data.setText(results.toString());
                                }

                            });

                        } catch (Exception e) {

                            data.setText("Error" + e);

                        }

                    }

                });

            }

        };
        nT.start();
    }



}

Solution

  • you should parse your XML with the help of xml pull parser

    Following code will guide you for doing so

    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 ;
        }
    

    Hope it may helps you