gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:04:53 25 4
gpt4 key购买 nike

在我的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 详细信息。请给我一个示例代码。

最佳答案

这将从 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
}

关于android - 如何在android中获取远程xml数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com