2012-10-05 30 views
0

我有一個Android應用程序從web服務調用中獲取xml響應。我正在嘗試使用DOM解析器解析xml。我無法獲取StartDate標記的值以及call標記中的其他標記。任何想法,我錯了。當我從startDate中記錄數值時,它是空的。謝謝。如何在Android中使用DOM解析xml字符串

public class DomParser { 

    private static final String TAG = DomParser.class.getSimpleName(); 
    DocumentBuilderFactory builderFactory; 
    DocumentBuilder builder; 
    Document document; 

    public DomParser() { 
     super(); 
     Log.e(TAG, "inside domparser constructor"); 
     builderFactory = DocumentBuilderFactory.newInstance(); 
     document = null; 
     try { 
      builder = builderFactory.newDocumentBuilder(); 
      Log.e(TAG, "built the dom factory"); 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } 
    }// end of constructor 


    public void parseXmlString(String str){ 

     try { 
      String s = str.replace("&lt;", "<"); 
      String t = s.replace("&gt;", ">"); 
      document = builder.parse(new InputSource(new StringReader(t))); 
      Log.e(TAG, "document = " + document); 
      Log.e(TAG, "str = " + t); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     Log.e(TAG, "about to parse the rootElement"); 
     Element rootElement = document.getDocumentElement(); 
     NodeList nodes = rootElement.getChildNodes(); 

     Node rota = nodes.item(0); 
     NodeList callList = rota.getChildNodes(); 

     for(int i = 0; i < callList.getLength(); i++){ 

      Node call = callList.item(i); 
      NodeList callChildrenList = call.getChildNodes(); 

      for(int j = 0; j < callChildrenList.getLength(); j++){ 
       Node callChild = callChildrenList.item(j); 
       if(callChild instanceof Element){ 
        //a child element to process 
        Element child = (Element) callChild; 
        String startDate = child.getAttribute("StartDate"); 
        Log.e(TAG, "element StartDate = " + startDate); 
        } 
      } 

     } 



    }//end of parseXmlString 

} 

<?xml version="1.0" encoding="utf-8"?> 
    10-05 15:49:17.680: E/DomParser(31495): <string xmlns="http://tempuri.org/"><Rota><Call><StartDate>2012-10-05T07:00:00+01:00</StartDate><EndDate>2012-10-05T07:25:00+01:00</EndDate><Duration>25</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Ebbs</ClientSurname><ClientForename>Vera</ClientForename><NeedName>Assist With Dressing, Laundry, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Prepare Breakfast, Shower, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>47418572-73a1-4dab-bc24-739c122b2b25</CallID><ActualTimeIn>07:30</ActualTimeIn><ActualTimeOut>07:49</ActualTimeOut></Call><Call><StartDate>2012-10-05T07:30:00+01:00</StartDate><EndDate>2012-10-05T08:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Brown</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>Careline, Diabetic Type 2, Empty Commode, KEY SAFE, Leave A Drink, Make A Hot Drink, Meds - Level II Administer, Prepare Snack, TIME BAND - Early Bed</NeedName><CarerAwayReason /><CallID>14f4f796-c7a6-4942-9d91-e9e7a40394f5</CallID><ActualTimeIn>08:00</ActualTimeIn><ActualTimeOut>08:21</ActualTimeOut></Call><Call><StartDate>2012-10-05T08:00:00+01:00</StartDate><EndDate>2012-10-05T08:40:00+01:00</EndDate><Duration>40</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Weaver</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>KEY SAFE, Morning Call - Get Up And Breakfast, Personal Care</NeedName><CarerAwayReason /><CallID>2a7e6c70-ee2c-4657-a313-a18b87a766ed</CallID><ActualTimeIn>08:35</ActualTimeIn><ActualTimeOut>09:13</ActualTimeOut></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:20:00+01:00</EndDate><Duration>20</Duration><CallStatusID>3</CallStatusID><CallStatusName>NCR</CallStatusName><ClientSurname>Howarth</ClientSurname><ClientForename>Edith</ClientForename><NeedName>Change Incontinence Pads, KEY SAFE, Make Bed/Change As Neccessary, Meds - Level II Administer, Personal Care, Prepare Breakfast, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>383c9be6-4cbe-459a-92ed-5dc309a0bffb</CallID></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:30:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Kastik</ClientSurname><ClientForename>Lydia</ClientForename><NeedName>Keys Needed, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Make Bed/Change As Neccessary, Medication Prompt, Meds - Level II Administer, Personal Care, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>f4cc1f96-ef65-4231-9e03-0b8263b04d27</CallID><ActualTimeIn>09:28</ActualTimeIn><ActualTimeOut>09:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T10:30:00+01:00</StartDate><EndDate>2012-10-05T11:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Terett</ClientSurname><ClientForename>Mamie</ClientForename><NeedName>Assist With Dressing, Meds - Self Medicating, Morning Call, Personal Care, Strip Wash, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>03f52182-b9ff-4fa9-9b71-1a20b8bbeeaa</CallID><ActualTimeIn>10:10</ActualTimeIn><ActualTimeOut>10:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:00:00+01:00</StartDate><EndDate>2012-10-05T12:15:00+01:00</EndDate><Duration>15</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Garland</ClientSurname><ClientForename>Mavis</ClientForename><NeedName>Empty Catheter Bag, Medication Prompt, Meds - Level II Administer, TIME BAND - Early Lunch, Toiletting/Commode</NeedName><CarerAwayReason /><CallID>802d8adf-3a80-40bd-be91-f29db7c1291e</CallID><ActualTimeIn>11:16</ActualTimeIn><ActualTimeOut>11:38</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:30:00+01:00</StartD 

[更新]

Element rootElement = document.getDocumentElement(); 
     NodeList nodes = rootElement.getChildNodes(); 

     Node rota = nodes.item(0); 
     NodeList callList = rota.getChildNodes(); 

     for(int i = 0; i < callList.getLength(); i++){ 

      Node call = callList.item(i); 
      NodeList callChildrenList = call.getChildNodes(); 


      for(int j = 0; j < callChildrenList.getLength(); j++){ 
       Node callChild = callChildrenList.item(j); 

       Log.e(TAG, "callChild name = " + callChild.getNodeName()); 


      } 

回答

2

像這樣基於新的示例代碼

// HERE LOOP TO FIND THE NODE LIKE YOU HAVE DONE IN YOUR EXAMPLE 
// WHERE YOU HAVE callChild.getNodeName() AND LOOK FOR THE ONE THAT IS "StartDate" 
// ASSUMING startDateNode HERE IS THE NODE YOU WANT 

    // GET THE CHILD NODES AND LOOK FOR THE TEXT NODE 
    NodeList nList = startDateNode.getChildNodes(); 
    for (int i=0; i < nList.getLength(); i++) { 
     Node tmpNode = nList.item(i); 
     if (tmpNode.getNodeType() == Node.TEXT_NODE) { 
      String dateStr = ((CharacterData)tmpNode).getData(); 
      // NOW YOU HAVE THE CHARACTER DATA 
      // DO SOMETHING WITH THE VALUE 
     } 
    } 
+0

嗨,有一個rota標籤。在rota標籤內有多個通話標籤。在每個呼叫標籤內部都有我需要獲取值的各種標籤。一旦我在調用標記中,每個後續值都必須填充到數組中。最終有一個數組是一個數組。例如在這種情況下,父數組將保存19個數組。這19個陣列中的每一個都將保存呼叫標籤內的數據。那麼你的代碼首先提取所有的startdates? – turtleboy

+0

我已更新我的帖子,並有點接近。我現在可以註銷所有標籤名稱,但是我將如何獲得它們的值?謝謝 – turtleboy

+0

我已經添加了一些錯誤的代碼,從其他的東西,我剛剛糾正。它變得糟糕,像這樣走DOM,但這應該有所幫助。您需要獲取現有節點的子節點,然後查找類型Node.TEXT_NODE,然後從中獲取數據。 –

1

check here來獲取正確的想法,我已經在我的代碼,這樣的一個實現。