简体   繁体   English

如何解析android中的xml响应?

[英]How to parse xml response in android?

In my application i am receiving the below Httpresponse from a webservice in xml format. 在我的应用程序中,我从xml格式的Web服务接收以下Httpresponse。

<?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? 如何提取xml标头的值?

Please Help ! 请帮忙 ! Thanks in Advance! 提前致谢!

Following is the code snippet for handling XML operations. 以下是用于处理XML操作的代码段。 I am using XML parser class so far we built and looping through each XML element and getting the child data. 到目前为止,我正在使用XML解析器类,我们构建并遍历每个XML元素并获取子数据。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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