我需要使用 android 后台服务从下面的 xml 中获取 mango 和 orange 的值。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><wsFruitResponse xmlns="http://www.orange.ws/fruitspec/ws">
<mango>wow</mango>
<orange>boom</orange>
</wsFruitResponse>
</soap:Body>
</soap:Envelope>
然后它应该存储在一个数组列表中。我怎样才能做到这一点。我习惯于常规的 sax 解析器,但这个肥皂的东西看起来很奇怪。请帮忙
类 loadingTask 扩展 AsyncTask {
protected String doInBackground(String... urls) {
...
sh.parseContent("");
return "";
}
protected void onPostExecute(String s) {
ShowProgress.dismiss();
}
}
class SAXHelper {
public HashMap<String, String> userList = new HashMap<String, String>();
private URL url2;
public SAXHelper(String url1) throws MalformedURLException {
this.url2 = new URL(url1);
}
public RSSHandler parseContent(String parseContent) {
RSSHandler df = new RSSHandler();
try {
....
xr.setContentHandler(df);
xr.parse(new InputSource(url2.openStream()));
} catch (Exception e) {
e.printStackTrace();
}
return df;
}
}
class RSSHandler extends DefaultHandler {
private Post currentPost = new Post();
StringBuffer chars = new StringBuffer();
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("item")) {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("orange")
&& currentPost.getOrange() == null) {
currentPost.setOrange(chars.toString());
}
if (localName.equalsIgnoreCase("mango")
&& currentPost.getMango() == null) {
currentPost.setMango(chars.toString());
}
if (localName.equalsIgnoreCase("item")) {
PostList.add(currentPost);
currentPost = new Post();
}
}
@Override
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
}