2013-07-22 3 views
0

Я понял, как работать с «нормальным» XML-деревом. Но я получаю следующую строку с сервером 3 партии:Как справиться с этой строкой 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"/> 

ответ

1
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"); 
} 

Вы можете получить атрибуты с именем, используя element.getAttribute() функцию.

Смежные вопросы