2015-12-09 37 views
0

我想解析從.net Web服務接收到的XML,並將解析的數據存儲到字符串數組中。我嘗試了很多次,但循環在一次執行後終止,沒有任何異常。適用於Android的Dom解析器

從Web服務接收到的XML附在此處。它用於解析

<DocumentElement> 
    <PictureList> 
     <Id>1</Id> 
     <WorkId>1</WorkId> 
     <PictureUrl>"~/Admin/"</PictureUrl> 
     <Status>Active</Status> 
     <CreatedDateTime>2015-11-21T00:00:00+00:00</CreatedDateTime> 
    </PictureList> 
</DocumentElement> 

代碼如下

public void pictures(String st) { 

     DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     builder = factory.newDocumentBuilder(); 
     Document doc = builder.parse(new InputSource(new StringReader(st))); 
     Element env = doc.getDocumentElement(); 
     NodeList nl = env.getElementsByTagName("PictureList"); 
     lengthsem = nl.getLength(); 
     length = 0; 
     Node n = nl.item(0); 
     NodeList nl_suc = n.getChildNodes(); 
     length = nl_suc.getLength(); 
     String[][] semres; 
     String semdata[][] = new String[lengthsem][36]; 
     for (int i = 0; i < lengthsem; i++) { 
      n = nl.item(i); 
      nl_suc = n.getChildNodes(); 
      length = nl_suc.getLength(); 
      int count = 0; 
      for (int j = 0; j < length; j++) { 
       System.out.println("length inndr" +length); 
       Node n_suc = nl_suc.item(j); 
       String lab = n_suc.getNodeName(); 
       Log.e("lab",lab); 
       Log.e("fhkhgfgfkhg", n_suc.getNodeName()); 
       if (n_suc.getNodeType() == Node.ELEMENT_NODE) { 
        sem = Webxml.getElementText((Element) n_suc); 
        Log.e("iloufhkhghjk,ldlfdlll", sem); 

        Log.e("fhkhghjk,ldlfdlll", lab); 
        Node hasSub = nl_suc.item(10); 
        Log.e("fhkhghjksunbb", hasSub.getNodeName()); 
        String sub = Webxml.getElementText((Element) hasSub); 
        if (lab.equals("Id")) { 
         semdata[i][0] = sem; 
         System.out.println("0 " + lab + i); 
         success = "true"; 
         try { 
          j++; 
          System.out.println(j + "jvalueee"); 
         }catch (Exception e){ 
          System.out.println("exception"); 
         } 
         Log.e("fhkhghjk,ldlfdlll", lab); 

        } else if (lab.equals("WorkId")) { 
         semdata[i][1] = sem; 
         System.out.println("0 " + lab + i); 
         success = "true"; 
         Log.e("fhkhghjk,ldlfdlll", lab); 
        } else if (lab.equals("PictureUrl")) { 
         semdata[i][2] = sem; 
         System.out.println("0 " + lab + i); 
         success = "true"; 
         Log.e("fhkhghjk,ldlfdlll", lab); 
        } else if (lab.equals("CreatedDateTime")) { 
         semdata[i][3] = sem; 
         System.out.println("0 " + lab + i); 
         success = "true"; 
         Log.e("fhkhghjk,ldlfdlll", lab); 

        } else { 
         System.out.println("test111"); 
         break; 
        } 
       } 
      } 
     } 
+1

你可以請包括你的代碼的相關部分?否則,我們將無法幫助您診斷問題。 – kiwidrew

+0

當然。解析代碼如下。並感謝您的即時迴應。 – Thaneerath

+0

@kiwidrew我附上相關部分作爲您的迴應。請幫幫我 – Thaneerath

回答

0

給出嗯,我的工作某種相同的數據,所以我可以建議你這樣。你也可以使用它。

String[][] strarray = new String[0][10]; 
    try { 

     mDbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = mDbf.newDocumentBuilder(); 
     is.setCharacterStream(new StringReader(xml)); 
     mDocument = db.parse(is); 
     mDocument.getDocumentElement().normalize(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    NodeList nodeList = mDocument.getElementsByTagName("PictureList"); 

    for (int i = 0; i < nodeList.getLength(); i++) { 
     Node node = nodeList.item(i); 


     if (node.getNodeType() == Node.ELEMENT_NODE) { 
      Element element = (Element) node; 
      strarray[0][i] = getValue("Id", element); 
      strarray[0][i] = getValue("WorkId", element)); 
      strarray[0][i] = getValue("PictureUrl", element)); 
      strarray[0][i] = getValue("Status", element)); 
      strarray[0][i] = getValue("CreatedDateTime", element)); 


     } 

    } 


public String getValue(String stag, Element mElement) { 
    String value = ""; 
    NodeList nlList = mElement.getElementsByTagName(stag).item(0) 
      .getChildNodes(); 
    Node nValue = (Node) nlList.item(0); 
    if (nValue != null) { 
     value = nValue.getTextContent(); 
    } 
    return value; 
} 

我改變它爲字符串數組。所以它可能在某個地方是邏輯錯誤的。