I figured out how to deal with a 'normal' XML-tree. But I receive the following string from a 3rd party server:
<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>
I'm retrieving a DOM-element with this method:
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;
}
And the results with this method:
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;
}
But when I try the same for this XML, I'm not getting the desired results. I want a List of calls with their destination, duration, cost. So basically I want the data between the "":
<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"/>
NodeList results = doc.getElementsByTagName("Call");
for (int i = 0; i < results.getLength(); i++) {
Element element = (Element) results.item(i);
String attribute= element.getAttribute("CallType");
String attribute2= element.getAttribute("Customer");
}
You can get attributes with name using element.getAttribute()
function.