如何在android中获取远程xml数据响应

时间:2012-10-04 20:10:24

标签: android xml web-services http

在我的remote server中,我有xml格式的数据

<reply>
<username>username</username>
<status>1<status>
<photo>http://diskonbanget.com/bni/images/user</photo>
<city><city_name>Bandung</city_name><city_code>1</city_code></city>
<jaminan>< jaminan_name>Rumah Tinggal</jaminan_name><jaminan_code>1</jaminan_code></ jaminan>
<dokumen><dokumen_name>SHM</dokumen_name><dokumen_code>1</dokumen_code></dokumen>
<property_list><property id=“1”>
    <description>bla bla</description>      <image>http://www.diskonbanget.com/bni/images/jaminan</image><property_code>1</property_code></property>
</property_list>
</reply>

所以,我需要将用户名,City_code,Jaminan_code和Dokumen_code作为参数传递  然后我需要从我的Android应用程序中读取城市细节,jaminan详细信息,dokumen详细信息。请给我一个示例代码。

2 个答案:

答案 0 :(得分:3)

这将从URL获取XML:

public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

这将从XML获取元素:

 public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
            // return DOM
        return doc;
}
public String getValue(Element item, String str) {
 NodeList n = item.getElementsByTagName(str);
 return this.getElementValue(n.item(0));
}

public final String getElementValue( Node elem ) {
     Node child;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                 if( child.getNodeType() == Node.TEXT_NODE  ){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
} 

用法:

// All static variables
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

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

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

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

答案 1 :(得分:0)

此外,如果您遇到charset问题,请使用getXmlFromUrl方法

xml = EntityUtils.toString(httpEntity, "your charset");

当我使用西里尔文xml数据时,它帮助了我。