「通常の」XML ツリーを処理する方法を理解しました。しかし、サードパーティのサーバーから次の文字列を受け取りました。
<CallOverview>
<Calls Count="2">
<Call CallType="GeoCall" Customer="this account" StartTime="2013-07-22 17:53:22 (UTC)" Destination="+123456789" Duration="00:00:14" Charge="0.00374" CallId="1472453365"/>
<Call CallType="GeoCall" Customer="this account" StartTime="2013-07-22 16:42:45 (UTC)" Destination="+123456789" Duration="00:00:05" Charge="0.00284" CallId="1472377565"/>
</Calls>
<MoreData>False</MoreData>
</CallOverview>
このメソッドで DOM 要素を取得しています。
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;
}
そして、この方法での結果:
Element e = (Element) nl.item(i); //nl is a nodelist of parent nodes
public HashMap<String, String> getResults(Element item) {
HashMap<String, String> map = new HashMap<String, String>();
NodeList results = item.getElementsByTagName(KEY_RESULT);
//I run through the node list:
map.put("RESPONSE", this.getElementValue(results.item(i)));
...
return map;
}
しかし、この XML に対して同じことを試みても、望ましい結果が得られません。宛先、期間、コストを含む通話のリストが必要です。したがって、基本的には「」の間のデータが必要です。
<Call CallType="GeoCall" Customer="this account" StartTime="2013-07-22 17:53:22 (UTC)" Destination="+123456789" Duration="00:00:14" Charge="0.00374" CallId="1472453365"/>