如何解析android中的xml响应?

时间:2016-05-12 06:00:51

标签: android xml

在我的应用程序中,我收到了来自xml格式的web服务的以下Httpresponse。

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

我尝试按以下格式解析响应:

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

但我得到的回应为null。

如何提取xml标头的值?

请帮忙!在此先感谢!

1 个答案:

答案 0 :(得分:2)

以下是用于处理XML操作的代码段。到目前为止,我正在使用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
}