简体   繁体   中英

How to parse xml response in android?

In my application i am receiving the below Httpresponse from a webservice in xml format.

<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<status>0</status>
<messageid>213123</messageid>
<destination>1234567890</destination>
</result>
</results>

I tried parsing the response in the below format :

    private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<SMSResponse> products = null;
    int eventType = parser.getEventType();
    SMSResponse currentProduct = null;

    while (eventType != XmlPullParser.END_DOCUMENT){
        Log.e("ENTER-", "while loop");
        String name = null;
        switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                products = new ArrayList<SMSResponse>();
                Log.e("ENTER-", "swicth case -1");
                break;
            case XmlPullParser.START_TAG:

                name = parser.getName();
                Log.e("start name", name);
                if(name.equals("results"))
                {
                     Log.e("start name", name);
                }
                else if(name.equals("result"))
                {
                     Log.e("start name", name);
                    currentProduct = new SMSResponse();
                }
                else if(name.equals("status"))
                {
                     Log.e("start name", name);
                     System.out.println("status value==");
                    currentProduct.status = parser.getProperty(name).toString();

                }
                else if(name.equals("messageid"))
                {
                     Log.e("start name", name);
                     System.out.println("messageid value=="+ parser.getProperty(name));
                    currentProduct.messageid = parser.getProperty(name).toString();
                }
                else if(name.equals("destination"))
                {
                     Log.e("start name", name);
                     System.out.println("destination value=="+ parser.getProperty(name));
                    currentProduct.destination = parser.getProperty(name).toString();
                }

                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                Log.e("end name", name);
//                    Log.e("ENTER-", "switch case-3");
                if (name.equalsIgnoreCase("results") ){
                     Log.e("end name", name);
                    products.add(currentProduct);
                } 
        }
        eventType = parser.next();
    }


    Log.e("Array", products.toString());

//        printProducts(products);
}

But i am getting null response as the value.

How do i extract the value of the xml header?

Please Help ! Thanks in Advance!

Following is the code snippet for handling XML operations. I am using XML parser class so far we built and looping through each XML element and getting the child data.

static final String KEY_RESULT = "result"; // parent node
static final String KEY_STATUS = "status";
static final String KEY_MESSAGE_ID = "messageid";
static final String KEY_DESC = "description";

XMLParser parser = new XMLParser();
//xml getting from url
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_RESULT);

// looping through all item nodes <item>      
for (int i = 0; i < nl.getLength(); i++) {
    String name = parser.getValue(e, KEY_STATUS); // name child value
    String cost = parser.getValue(e, KEY_MESSAGE_ID); // cost child value
    String description = parser.getValue(e, KEY_DESC); // description child value
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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