2

在我的 远程服务器中,我有 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”&gt;
    <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 详细信息。请给我一个示例代码。

4

2 回答 2

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
}
于 2012-10-04T20:29:26.097 回答
0

另外,如果你对 charset 有问题,使用 ingetXmlFromUrl方法

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

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

于 2013-08-06T08:03:35.660 回答